new generated file + genscript + skeleton of interpreter
This commit is contained in:
@@ -18,7 +18,9 @@ OBJ_DIR = obj
|
||||
|
||||
SOURCES = \
|
||||
$(sort \
|
||||
$(wildcard ./src/*.c))
|
||||
$(wildcard ./src/*.c)\
|
||||
$(wildcard ./src/libperipherals/*.c)\
|
||||
$(wildcard ./src/libcoreint/*.c))
|
||||
|
||||
INCLUDES = \
|
||||
-I src \
|
||||
@@ -48,7 +50,7 @@ CFLAGS ?= $(INCLUDES) -std=c99 -m32 -fdiagnostics-color=always
|
||||
#CFLAGS += -ffunction-sections -fdata-sections
|
||||
|
||||
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
|
||||
|
||||
|
||||
+10
-35
@@ -14,41 +14,16 @@
|
||||
*/
|
||||
|
||||
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"
|
||||
"setTimeout (LEDsOn, 500);\n"
|
||||
"setTimeout (LEDsOff, 500);\n"
|
||||
"LEDToggle (LED3);\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"
|
||||
;
|
||||
|
||||
+62
-16
@@ -263,29 +263,48 @@ parse_name ()
|
||||
if (kw != KW_NONE)
|
||||
{
|
||||
free ((char *) tok);
|
||||
return (token) { .type = TOK_KEYWORD, .data.kw = kw };
|
||||
|
||||
return (token)
|
||||
{
|
||||
.type = TOK_KEYWORD, .data.kw = kw
|
||||
};
|
||||
}
|
||||
|
||||
if (!strcmp ("null", tok))
|
||||
{
|
||||
free ((char *) tok);
|
||||
return (token) { .type = TOK_NULL, .data.none = NULL };
|
||||
|
||||
return (token)
|
||||
{
|
||||
.type = TOK_NULL, .data.none = NULL
|
||||
};
|
||||
}
|
||||
|
||||
if (!strcmp ("true", 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))
|
||||
{
|
||||
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
|
||||
@@ -376,7 +395,11 @@ parse_number ()
|
||||
res = (res << 4) + hex_to_int (tok[i]);
|
||||
|
||||
free ((char *) tok);
|
||||
return (token) { .type = TOK_INT, .data.num = res };
|
||||
|
||||
return (token)
|
||||
{
|
||||
.type = TOK_INT, .data.num = res
|
||||
};
|
||||
}
|
||||
|
||||
assert (!is_hex && !is_exp);
|
||||
@@ -429,7 +452,11 @@ parse_number ()
|
||||
tok = current_token ();
|
||||
float res = strtof (tok, NULL);
|
||||
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;
|
||||
@@ -439,7 +466,11 @@ parse_number ()
|
||||
res = res * 10 + hex_to_int (tok[i]);
|
||||
|
||||
free ((char *) tok);
|
||||
return (token) { .type = TOK_INT, .data.num = res };
|
||||
|
||||
return (token)
|
||||
{
|
||||
.type = TOK_INT, .data.num = res
|
||||
};
|
||||
}
|
||||
|
||||
static char
|
||||
@@ -513,12 +544,12 @@ parse_string ()
|
||||
{
|
||||
if (*i == '\\')
|
||||
{
|
||||
if (*(i+1) == '\n')
|
||||
if (*(i + 1) == '\n')
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
*index = escape_char (*(i+1));
|
||||
*index = escape_char (*(i + 1));
|
||||
index++;
|
||||
i++;
|
||||
continue;
|
||||
@@ -534,7 +565,10 @@ parse_string ()
|
||||
// Eat up '"'
|
||||
consume_char ();
|
||||
|
||||
return (token) { .type = TOK_STRING, .data.str = tok };
|
||||
return (token)
|
||||
{
|
||||
.type = TOK_STRING, .data.str = tok
|
||||
};
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -554,7 +588,9 @@ lexer_set_file (FILE *ex_file)
|
||||
{
|
||||
assert (ex_file);
|
||||
file = ex_file;
|
||||
#ifdef DEBUG
|
||||
lexer_debug_log = fopen ("lexer.log", "w");
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool
|
||||
@@ -621,11 +657,18 @@ lexer_next_token ()
|
||||
if (c == '\n')
|
||||
{
|
||||
consume_char ();
|
||||
return (token) { .type = TOK_NEWLINE, .data.none = NULL };
|
||||
|
||||
return (token)
|
||||
{
|
||||
.type = TOK_NEWLINE, .data.none = NULL
|
||||
};
|
||||
}
|
||||
|
||||
if (c == '\0')
|
||||
return (token) { .type = TOK_EOF, .data.none = NULL };
|
||||
return (token)
|
||||
{
|
||||
.type = TOK_EOF, .data.none = NULL
|
||||
};
|
||||
|
||||
if (c == '\'' || c == '"')
|
||||
return parse_string ();
|
||||
@@ -639,7 +682,10 @@ lexer_next_token ()
|
||||
if (c == '/' && LA (1) == '*')
|
||||
{
|
||||
if (replace_comment_by_newline ())
|
||||
return (token) { .type = TOK_NEWLINE, .data.none = NULL };
|
||||
return (token)
|
||||
{
|
||||
.type = TOK_NEWLINE, .data.none = NULL
|
||||
};
|
||||
else
|
||||
return lexer_next_token ();
|
||||
}
|
||||
@@ -737,10 +783,10 @@ lexer_next_token ()
|
||||
void
|
||||
lexer_save_token (token tok)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
#ifdef DEBUG
|
||||
if (tok.type == TOK_CLOSE_BRACE)
|
||||
fprintf (lexer_debug_log, "lexer_save_token(%d): type=0x%x, data=%p\n", i, tok.type, tok.data.none);
|
||||
#endif
|
||||
#endif
|
||||
saved_token = tok;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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
@@ -18,13 +18,22 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "error.h"
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
#include "pretty-printer.h"
|
||||
|
||||
#include "ctx-manager.h"
|
||||
#include "mem-allocator.h"
|
||||
|
||||
#include "interpreter.h"
|
||||
|
||||
#include "generated.h"
|
||||
|
||||
void fake_exit ();
|
||||
|
||||
void
|
||||
fake_exit (void)
|
||||
{
|
||||
for (;;);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@@ -58,36 +67,17 @@ main (int argc, char **argv)
|
||||
file = fopen (file_name, "r");
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
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)
|
||||
{
|
||||
statement *st;
|
||||
lexer_set_file (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 ();
|
||||
}
|
||||
//gen_bytecode (generated_source);
|
||||
gen_bytecode (file);
|
||||
run_int ();
|
||||
|
||||
#ifdef __TARGET_MCU
|
||||
fake_exit ();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Vendored
+26
@@ -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);
|
||||
}
|
||||
Executable
+22
@@ -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"
|
||||
Reference in New Issue
Block a user