new generated file + genscript + skeleton of interpreter

This commit is contained in:
e.gavrin
2014-07-02 16:42:03 +04:00
parent 604771248f
commit eb38816fc4
10 changed files with 651 additions and 376 deletions
+4 -2
View File
@@ -18,7 +18,9 @@ OBJ_DIR = obj
SOURCES = \ SOURCES = \
$(sort \ $(sort \
$(wildcard ./src/*.c)) $(wildcard ./src/*.c)\
$(wildcard ./src/libperipherals/*.c)\
$(wildcard ./src/libcoreint/*.c))
INCLUDES = \ INCLUDES = \
-I src \ -I src \
@@ -48,7 +50,7 @@ CFLAGS ?= $(INCLUDES) -std=c99 -m32 -fdiagnostics-color=always
#CFLAGS += -ffunction-sections -fdata-sections #CFLAGS += -ffunction-sections -fdata-sections
DEBUG_OPTIONS = -g3 -O0 -DDEBUG #-fsanitize=address DEBUG_OPTIONS = -g3 -O0 -DDEBUG #-fsanitize=address
RELEASE_OPTIONS = -Os -Werror RELEASE_OPTIONS = -Os # -Werror
DEFINES = -DMEM_HEAP_CHUNK_SIZE=256 -DMEM_HEAP_AREA_SIZE=32768 DEFINES = -DMEM_HEAP_CHUNK_SIZE=256 -DMEM_HEAP_AREA_SIZE=32768
+10 -35
View File
@@ -14,41 +14,16 @@
*/ */
static const char* generated_source = "" static const char* generated_source = ""
"// AST\n"
"// ECMA has no way of including files. Do we need such feature?\n"
"// EG: Not in initial version\n"
"require (leds);\n"
"\n"
"function LEDsOn () {\n"
"LEDOn (LED3);\n"
"LEDOn (LED6);\n"
"LEDOn (LED7);\n"
"LEDOn (LED4);\n"
"LEDOn (LED10);\n"
"LEDOn (LED8);\n"
"LEDOn (LED9);\n"
"LEDOn (LED5);\n"
"}\n"
"\n"
"function LEDsOff () {\n"
"LEDOff (LED3);\n"
"LEDOff (LED6);\n"
"LEDOff (LED7);\n"
"LEDOff (LED4);\n"
"LEDOff (LED10);\n"
"LEDOff (LED8);\n"
"LEDOff (LED9);\n"
"LEDOff (LED5);\n"
"}\n"
"\n"
"/*\n"
"IMHO we don't need this function in our code,\n"
"we may perform lazy LEDs initialization.\n"
"*/\n"
"// initLEDs ();\n"
"\n"
"while (true) {\n" "while (true) {\n"
"setTimeout (LEDsOn, 500);\n" "LEDToggle (LED3);\n"
"setTimeout (LEDsOff, 500);\n" "LEDToggle (LED6);\n"
"LEDToggle (LED7);\n"
"LEDToggle (LED4);\n"
"LEDToggle (LED10);\n"
"LEDToggle (LED8);\n"
"LEDToggle (LED9);\n"
"LEDToggle (LED5);\n"
"\n"
"wait(500);\n"
"}\n" "}\n"
; ;
+58 -12
View File
@@ -263,29 +263,48 @@ parse_name ()
if (kw != KW_NONE) if (kw != KW_NONE)
{ {
free ((char *) tok); free ((char *) tok);
return (token) { .type = TOK_KEYWORD, .data.kw = kw };
return (token)
{
.type = TOK_KEYWORD, .data.kw = kw
};
} }
if (!strcmp ("null", tok)) if (!strcmp ("null", tok))
{ {
free ((char *) tok); free ((char *) tok);
return (token) { .type = TOK_NULL, .data.none = NULL };
return (token)
{
.type = TOK_NULL, .data.none = NULL
};
} }
if (!strcmp ("true", tok)) if (!strcmp ("true", tok))
{ {
free ((char *) tok); free ((char *) tok);
return (token) { .type = TOK_BOOL, .data.is_true = true };
return (token)
{
.type = TOK_BOOL, .data.is_true = true
};
} }
if (!strcmp ("false", tok)) if (!strcmp ("false", tok))
{ {
free ((char *) tok); free ((char *) tok);
return (token) { .type = TOK_BOOL, .data.is_true = false };
return (token)
{
.type = TOK_BOOL, .data.is_true = false
};
} }
} }
return (token) { .type = TOK_NAME, .data.name = tok }; return (token)
{
.type = TOK_NAME, .data.name = tok
};
} }
static bool static bool
@@ -376,7 +395,11 @@ parse_number ()
res = (res << 4) + hex_to_int (tok[i]); res = (res << 4) + hex_to_int (tok[i]);
free ((char *) tok); free ((char *) tok);
return (token) { .type = TOK_INT, .data.num = res };
return (token)
{
.type = TOK_INT, .data.num = res
};
} }
assert (!is_hex && !is_exp); assert (!is_hex && !is_exp);
@@ -429,7 +452,11 @@ parse_number ()
tok = current_token (); tok = current_token ();
float res = strtof (tok, NULL); float res = strtof (tok, NULL);
free ((char *) tok); free ((char *) tok);
return (token) { .type = TOK_FLOAT, .data.fp_num = res };
return (token)
{
.type = TOK_FLOAT, .data.fp_num = res
};
} }
tok_length = buffer - token_start; tok_length = buffer - token_start;
@@ -439,7 +466,11 @@ parse_number ()
res = res * 10 + hex_to_int (tok[i]); res = res * 10 + hex_to_int (tok[i]);
free ((char *) tok); free ((char *) tok);
return (token) { .type = TOK_INT, .data.num = res };
return (token)
{
.type = TOK_INT, .data.num = res
};
} }
static char static char
@@ -534,7 +565,10 @@ parse_string ()
// Eat up '"' // Eat up '"'
consume_char (); consume_char ();
return (token) { .type = TOK_STRING, .data.str = tok }; return (token)
{
.type = TOK_STRING, .data.str = tok
};
} }
static void static void
@@ -554,7 +588,9 @@ lexer_set_file (FILE *ex_file)
{ {
assert (ex_file); assert (ex_file);
file = ex_file; file = ex_file;
#ifdef DEBUG
lexer_debug_log = fopen ("lexer.log", "w"); lexer_debug_log = fopen ("lexer.log", "w");
#endif
} }
static bool static bool
@@ -621,11 +657,18 @@ lexer_next_token ()
if (c == '\n') if (c == '\n')
{ {
consume_char (); consume_char ();
return (token) { .type = TOK_NEWLINE, .data.none = NULL };
return (token)
{
.type = TOK_NEWLINE, .data.none = NULL
};
} }
if (c == '\0') if (c == '\0')
return (token) { .type = TOK_EOF, .data.none = NULL }; return (token)
{
.type = TOK_EOF, .data.none = NULL
};
if (c == '\'' || c == '"') if (c == '\'' || c == '"')
return parse_string (); return parse_string ();
@@ -639,7 +682,10 @@ lexer_next_token ()
if (c == '/' && LA (1) == '*') if (c == '/' && LA (1) == '*')
{ {
if (replace_comment_by_newline ()) if (replace_comment_by_newline ())
return (token) { .type = TOK_NEWLINE, .data.none = NULL }; return (token)
{
.type = TOK_NEWLINE, .data.none = NULL
};
else else
return lexer_next_token (); return lexer_next_token ();
} }
+87
View File
@@ -0,0 +1,87 @@
/* Copyright 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "error.h"
#include "lexer.h"
#include "parser.h"
#include "pretty-printer.h"
#include "interpreter.h"
#include "opcode.h"
void
safe_opcode (FILE *file, opcode_ptr ptr, int arg1, int arg2)
{
curr_opcode.func = ptr;
curr_opcode.arg1 = arg1;
curr_opcode.arg2 = arg2;
if (file != NULL)
{
fwrite (&curr_opcode, sizeof (struct opcode_packed), 1, file);
}
}
void
gen_bytecode (FILE *src_file)
{
statement *st;
lexer_set_file (src_file);
parser_init ();
st = parser_parse_statement ();
assert (st);
while (st->type != STMT_EOF)
{
pp_statement (st);
st = parser_parse_statement ();
assert (st);
}
pp_finish ();
FILE *file = fopen (FILE_NAME, "w+b");
int i;
for (i = 0; i <= 10; i++)
{
safe_opcode (file, control_op, i, i);
safe_opcode (file, decl_op, i, i);
safe_opcode (file, call_op, i, i);
}
fclose (file);
}
void
run_int ()
{
FILE *file = fopen (FILE_NAME, "rb");
if (file == NULL)
{
fputs ("File error", stderr);
exit (1);
}
while (!feof (file))
{
fread (&curr_opcode, sizeof (struct opcode_packed), 1, file);
//printf ("read %d, %d, %p\n", curr_opcode.arg1, curr_opcode.arg2, curr_opcode.func);
curr_opcode.func (curr_opcode.arg1, curr_opcode.arg2);
}
fclose (file);
}
+39
View File
@@ -0,0 +1,39 @@
/* Copyright 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* File: interpreter.h
* Author: egavrin
*
* Created on July 2, 2014, 3:10 PM
*/
#ifndef INTERPRETER_H
#define INTERPRETER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "opcode.h"
#define FILE_NAME "application.bin"
void safe_opcode(FILE *, opcode_ptr, int, int);
void gen_bytecode(FILE*);
void run_int();
#endif /* INTERPRETER_H */
+33
View File
@@ -0,0 +1,33 @@
/* Copyright 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "opcode.h"
#include <stdio.h>
OP_DEFINITION (control_op)
{
printf ("control_op %d, %d\n", arg1, arg2);
}
OP_DEFINITION (decl_op)
{
printf ("decl_op %d, %d\n", arg1, arg2);
}
OP_DEFINITION (call_op)
{
printf ("call_op %d, %d\n", arg1, arg2);
}
+55
View File
@@ -0,0 +1,55 @@
/* Copyright 2014 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* File: opcode.h
* Author: egavrin
*
* Created on July 2, 2014, 3:12 PM
*/
#ifndef OPCODE_H
#define OPCODE_H
#define OP_RET_TYPE void
#define OP_ARG_TYPE int
#define OP_ATTR_DECL OP_ARG_TYPE, OP_ARG_TYPE
#define OP_ATTR_DEF OP_ARG_TYPE arg1, OP_ARG_TYPE arg2
#define OP_DECLARATION(opname) OP_RET_TYPE opname (OP_ATTR_DECL)
#define OP_DEFINITION(opname) OP_RET_TYPE opname (OP_ATTR_DEF)
// opcode ptr
typedef OP_RET_TYPE (*opcode_ptr)(OP_ATTR_DECL);
struct
__attribute__ ((__packed__))
opcode_packed
{
opcode_ptr func;
OP_ARG_TYPE arg1;
OP_ARG_TYPE arg2;
}
curr_opcode;
#define INIT_OPCODE_PTR(func) opcode_ptr func_ptr = ptr;
OP_DECLARATION (decl_op);
OP_DECLARATION (call_op);
OP_DECLARATION (control_op);
#endif /* OPCODE_H */
+20 -30
View File
@@ -18,13 +18,22 @@
#include <string.h> #include <string.h>
#include "error.h" #include "error.h"
#include "lexer.h"
#include "parser.h"
#include "pretty-printer.h"
#include "ctx-manager.h" #include "ctx-manager.h"
#include "mem-allocator.h" #include "mem-allocator.h"
#include "interpreter.h"
#include "generated.h"
void fake_exit ();
void
fake_exit (void)
{
for (;;);
}
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
@@ -58,36 +67,17 @@ main (int argc, char **argv)
file = fopen (file_name, "r"); file = fopen (file_name, "r");
if (file == NULL) if (file == NULL)
{
fatal (ERR_IO); fatal (ERR_IO);
if (dump_tokens)
{
token tok;
lexer_set_file (file);
tok = lexer_next_token ();
pp_reset ();
while (tok.type != TOK_EOF)
{
pp_token (tok);
tok = lexer_next_token ();
}
} }
if (dump_ast) //gen_bytecode (generated_source);
{ gen_bytecode (file);
statement *st; run_int ();
lexer_set_file (file);
parser_init (); #ifdef __TARGET_MCU
st = parser_parse_statement (); fake_exit ();
assert (st); #endif
while (st->type != STMT_EOF)
{
pp_statement (st);
st = parser_parse_statement ();
assert (st);
}
pp_finish ();
}
return 0; return 0;
} }
+26
View File
@@ -0,0 +1,26 @@
// Copyright 2014 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
while (true) {
LEDToggle (LED3);
LEDToggle (LED6);
LEDToggle (LED7);
LEDToggle (LED4);
LEDToggle (LED10);
LEDToggle (LED8);
LEDToggle (LED9);
LEDToggle (LED5);
wait(500);
}
+22
View File
@@ -0,0 +1,22 @@
# Copyright 2014 Samsung Electronics Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/bin/bash
echo "static const char* generated_source = \"\"" > "generated.h"
while read line
do
echo "\"$line\n\"" >> "generated.h"
done < $1
echo ";" >> "generated.h"