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"
; ;
+355 -309
View File
@@ -116,55 +116,55 @@ get_char (int i)
assert (file); assert (file);
if (buffer == NULL) if (buffer == NULL)
{
buffer = (char *) malloc (BUFFER_SIZE);
error = fread (buffer, 1, BUFFER_SIZE, file);
if (error < 0)
fatal (ERR_IO);
if (error == 0)
return '\0';
if (error < BUFFER_SIZE)
memset (buffer + error, '\0', BUFFER_SIZE - error);
buffer_start = buffer;
}
if (tail_size <= i)
{
/* We are almost at the end of the buffer. */
if (token_start)
{ {
buffer = (char *) malloc (BUFFER_SIZE); const int token_size = buffer - token_start;
error = fread (buffer, 1, BUFFER_SIZE, file); /* Whole buffer contains single token. */
if (token_start == buffer_start)
fatal (ERR_BUFFER_SIZE);
/* Move parsed token and tail of buffer to head. */
memmove (buffer_start, token_start, tail_size + token_size);
/* Adjust pointers. */
token_start = buffer_start;
buffer = buffer_start + token_size;
/* Read more characters form input file. */
error = fread (buffer + tail_size, 1, BUFFER_SIZE - tail_size - token_size, file);
if (error < 0) if (error < 0)
fatal (ERR_IO); fatal (ERR_IO);
if (error == 0) if (error == 0)
return '\0'; return '\0';
if (error < BUFFER_SIZE) if (error < BUFFER_SIZE - tail_size - token_size)
memset (buffer + error, '\0', BUFFER_SIZE - error); memset (buffer + tail_size + error, '\0',
buffer_start = buffer; BUFFER_SIZE - tail_size - token_size - error);
} }
else
if (tail_size <= i)
{ {
/* We are almost at the end of the buffer. */ memmove (buffer_start, buffer, tail_size);
if (token_start) buffer = buffer_start;
{ error = fread (buffer + tail_size, 1, BUFFER_SIZE - tail_size, file);
const int token_size = buffer - token_start; if (error < 0)
/* Whole buffer contains single token. */ fatal (ERR_IO);
if (token_start == buffer_start) if (error == 0)
fatal (ERR_BUFFER_SIZE); return '\0';
/* Move parsed token and tail of buffer to head. */ if (error < BUFFER_SIZE - tail_size)
memmove (buffer_start, token_start, tail_size + token_size); memset (buffer + tail_size + error, '\0', BUFFER_SIZE - tail_size - error);
/* Adjust pointers. */
token_start = buffer_start;
buffer = buffer_start + token_size;
/* Read more characters form input file. */
error = fread (buffer + tail_size, 1, BUFFER_SIZE - tail_size - token_size, file);
if (error < 0)
fatal (ERR_IO);
if (error == 0)
return '\0';
if (error < BUFFER_SIZE - tail_size - token_size)
memset (buffer + tail_size + error, '\0',
BUFFER_SIZE - tail_size - token_size - error);
}
else
{
memmove (buffer_start, buffer, tail_size);
buffer = buffer_start;
error = fread (buffer + tail_size, 1, BUFFER_SIZE - tail_size, file);
if (error < 0)
fatal (ERR_IO);
if (error == 0)
return '\0';
if (error < BUFFER_SIZE - tail_size)
memset (buffer + tail_size + error, '\0', BUFFER_SIZE - tail_size - error);
}
} }
}
return *(buffer + i); return *(buffer + i);
} }
@@ -245,55 +245,74 @@ parse_name ()
new_token (); new_token ();
consume_char (); consume_char ();
while (true) while (true)
{ {
c = LA (0); c = LA (0);
if (c == '\0') if (c == '\0')
c = c; c = c;
if (!isalpha (c) && !isdigit (c) && c != '$' && c != '_') if (!isalpha (c) && !isdigit (c) && c != '$' && c != '_')
break; break;
if (every_char_islower && (!isalpha (c) || !islower (c))) if (every_char_islower && (!isalpha (c) || !islower (c)))
every_char_islower = false; every_char_islower = false;
consume_char (); consume_char ();
} }
tok = current_token (); tok = current_token ();
if (every_char_islower) if (every_char_islower)
{
keyword kw = decode_keyword (tok);
if (kw != KW_NONE)
{ {
keyword kw = decode_keyword (tok); free ((char *) tok);
if (kw != KW_NONE)
{
free ((char *) tok);
return (token) { .type = TOK_KEYWORD, .data.kw = kw };
}
if (!strcmp ("null", tok)) return (token)
{ {
free ((char *) tok); .type = TOK_KEYWORD, .data.kw = kw
return (token) { .type = TOK_NULL, .data.none = NULL }; };
}
if (!strcmp ("true", tok))
{
free ((char *) tok);
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_NAME, .data.name = tok }; if (!strcmp ("null", tok))
{
free ((char *) tok);
return (token)
{
.type = TOK_NULL, .data.none = NULL
};
}
if (!strcmp ("true", tok))
{
free ((char *) tok);
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_NAME, .data.name = tok
};
} }
static bool static bool
is_hex_digit (char c) is_hex_digit (char c)
{ {
return isdigit (c) || c == 'a' || c == 'A' || c == 'b' || c == 'B' return isdigit (c) || c == 'a' || c == 'A' || c == 'b' || c == 'B'
|| c == 'c' || c == 'C' || c == 'd' || c == 'D' || c == 'c' || c == 'C' || c == 'd' || c == 'D'
|| c == 'e' || c == 'E' || c == 'f' || c == 'F'; || c == 'e' || c == 'E' || c == 'f' || c == 'F';
} }
static int static int
@@ -301,29 +320,29 @@ hex_to_int (char hex)
{ {
switch (hex) switch (hex)
{ {
case '0': return 0x0; case '0': return 0x0;
case '1': return 0x1; case '1': return 0x1;
case '2': return 0x2; case '2': return 0x2;
case '3': return 0x3; case '3': return 0x3;
case '4': return 0x4; case '4': return 0x4;
case '5': return 0x5; case '5': return 0x5;
case '6': return 0x6; case '6': return 0x6;
case '7': return 0x7; case '7': return 0x7;
case '8': return 0x8; case '8': return 0x8;
case '9': return 0x9; case '9': return 0x9;
case 'a': case 'a':
case 'A': return 0xA; case 'A': return 0xA;
case 'b': case 'b':
case 'B': return 0xB; case 'B': return 0xB;
case 'c': case 'c':
case 'C': return 0xC; case 'C': return 0xC;
case 'd': case 'd':
case 'D': return 0xD; case 'D': return 0xD;
case 'e': case 'e':
case 'E': return 0xE; case 'E': return 0xE;
case 'f': case 'f':
case 'F': return 0xF; case 'F': return 0xF;
default: unreachable (); default: unreachable ();
} }
} }
@@ -347,38 +366,42 @@ parse_number ()
is_hex = true; is_hex = true;
if (c == '.') if (c == '.')
{ {
assert (!isalpha (LA (1))); assert (!isalpha (LA (1)));
is_fp = true; is_fp = true;
} }
if (is_hex) if (is_hex)
{
// Eat up '0x'
consume_char ();
consume_char ();
new_token ();
while (true)
{ {
// Eat up '0x' c = LA (0);
if (!is_hex_digit (c))
break;
consume_char (); consume_char ();
consume_char ();
new_token ();
while (true)
{
c = LA (0);
if (!is_hex_digit (c))
break;
consume_char ();
}
if (isalpha (c) || c == '_' || c == '$')
fatal (ERR_INT_LITERAL);
tok_length = buffer - token_start;
tok = current_token ();
// OK, I know that integer overflow can occur here
for (int i = 0; i < tok_length; i++)
res = (res << 4) + hex_to_int (tok[i]);
free ((char *) tok);
return (token) { .type = TOK_INT, .data.num = res };
} }
if (isalpha (c) || c == '_' || c == '$')
fatal (ERR_INT_LITERAL);
tok_length = buffer - token_start;
tok = current_token ();
// OK, I know that integer overflow can occur here
for (int i = 0; i < tok_length; i++)
res = (res << 4) + hex_to_int (tok[i]);
free ((char *) tok);
return (token)
{
.type = TOK_INT, .data.num = res
};
}
assert (!is_hex && !is_exp); assert (!is_hex && !is_exp);
new_token (); new_token ();
@@ -388,49 +411,53 @@ parse_number ()
consume_char (); consume_char ();
while (true) while (true)
{
c = LA (0);
if (is_fp && c == '.')
fatal (ERR_INT_LITERAL);
if (is_exp && (c == 'e' || c == 'E'))
fatal (ERR_INT_LITERAL);
if (c == '.')
{ {
c = LA (0); if (isalpha (LA (1)) || LA (1) == '_' || LA (1) == '$')
if (is_fp && c == '.')
fatal (ERR_INT_LITERAL); fatal (ERR_INT_LITERAL);
if (is_exp && (c == 'e' || c == 'E')) is_fp = true;
fatal (ERR_INT_LITERAL);
if (c == '.')
{
if (isalpha (LA (1)) || LA (1) == '_' || LA (1) == '$')
fatal (ERR_INT_LITERAL);
is_fp = true;
consume_char ();
continue;
}
if (c == 'e' || c == 'E')
{
if (LA (1) == '-' || LA (1) == '+')
consume_char ();
if (!isdigit (LA (1)))
fatal (ERR_INT_LITERAL);
is_exp = true;
consume_char ();
continue;
}
if (isalpha (c) || c == '_' || c == '$')
fatal (ERR_INT_LITERAL);
if (!isdigit (c))
break;
consume_char (); consume_char ();
continue;
} }
if (c == 'e' || c == 'E')
{
if (LA (1) == '-' || LA (1) == '+')
consume_char ();
if (!isdigit (LA (1)))
fatal (ERR_INT_LITERAL);
is_exp = true;
consume_char ();
continue;
}
if (isalpha (c) || c == '_' || c == '$')
fatal (ERR_INT_LITERAL);
if (!isdigit (c))
break;
consume_char ();
}
if (is_fp || is_exp) if (is_fp || is_exp)
{
tok = current_token ();
float res = strtof (tok, NULL);
free ((char *) tok);
return (token)
{ {
tok = current_token (); .type = TOK_FLOAT, .data.fp_num = res
float res = strtof (tok, NULL); };
free ((char *) tok); }
return (token) { .type = TOK_FLOAT, .data.fp_num = res };
}
tok_length = buffer - token_start; tok_length = buffer - token_start;
tok = current_token (); tok = current_token ();
@@ -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
@@ -447,16 +478,16 @@ escape_char (char c)
{ {
switch (c) switch (c)
{ {
case 'b': return '\b'; case 'b': return '\b';
case 'f': return '\f'; case 'f': return '\f';
case 'n': return '\n'; case 'n': return '\n';
case 'r': return '\r'; case 'r': return '\r';
case 't': return '\t'; case 't': return '\t';
case 'v': return '\v'; case 'v': return '\v';
case '\'': case '\'':
case '"': case '"':
case '\\': case '\\':
default: return c; default: return c;
} }
} }
@@ -478,63 +509,66 @@ parse_string ()
new_token (); new_token ();
while (true) while (true)
{
c = LA (0);
if (c == '\0')
fatal (ERR_UNCLOSED);
if (c == '\n')
fatal (ERR_STRING);
if (c == '\\')
{ {
c = LA (0); /* Only single escape character is allowed. */
if (c == '\0') if (LA (1) == 'x' || LA (1) == 'u' || isdigit (LA (1)))
fatal (ERR_UNCLOSED);
if (c == '\n')
fatal (ERR_STRING); fatal (ERR_STRING);
if (c == '\\') if ((LA (1) == '\'' && !is_double_quoted)
{ || (LA (1) == '"' && is_double_quoted)
/* Only single escape character is allowed. */ || LA (1) == '\n')
if (LA (1) == 'x' || LA (1) == 'u' || isdigit (LA (1))) {
fatal (ERR_STRING); consume_char ();
if ((LA (1) == '\'' && !is_double_quoted) consume_char ();
|| (LA (1) == '"' && is_double_quoted) continue;
|| LA (1) == '\n') }
{
consume_char ();
consume_char ();
continue;
}
}
else if ((c == '\'' && !is_double_quoted)
|| (c == '"' && is_double_quoted))
break;
consume_char ();
} }
else if ((c == '\'' && !is_double_quoted)
|| (c == '"' && is_double_quoted))
break;
consume_char ();
}
length = buffer - token_start; length = buffer - token_start;
tok = (char *) malloc (length); tok = (char *) malloc (length);
index = tok; index = tok;
for (char *i = token_start; i < buffer; i++) for (char *i = token_start; i < buffer; i++)
{
if (*i == '\\')
{ {
if (*i == '\\') if (*(i + 1) == '\n')
{ {
if (*(i+1) == '\n') i++;
{ continue;
i++; }
continue; *index = escape_char (*(i + 1));
}
*index = escape_char (*(i+1));
index++;
i++;
continue;
}
*index = *i;
index++; index++;
i++;
continue;
} }
*index = *i;
index++;
}
memset (index, '\0', length - (index - tok)); memset (index, '\0', length - (index - tok));
token_start = NULL; token_start = NULL;
// 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
@@ -543,10 +577,10 @@ grobble_whitespaces ()
char c = LA (0); char c = LA (0);
while ((isspace (c) && c != '\n') || c == '\0') while ((isspace (c) && c != '\n') || c == '\0')
{ {
consume_char (); consume_char ();
c = LA (0); c = LA (0);
} }
} }
void 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
@@ -573,25 +609,25 @@ replace_comment_by_newline ()
consume_char (); consume_char ();
while (true) while (true)
{
c = LA (0);
if (!multiline && (c == '\n' || c == '\0'))
return false;
if (multiline && c == '*' && LA (1) == '/')
{ {
c = LA (0);
if (!multiline && (c == '\n' || c == '\0'))
return false;
if (multiline && c == '*' && LA (1) == '/')
{
consume_char ();
consume_char ();
if (was_newlines)
return true;
else
return false;
}
if (multiline && c == '\n')
was_newlines = true;
if (multiline && c == '\0')
fatal (ERR_UNCLOSED);
consume_char (); consume_char ();
consume_char ();
if (was_newlines)
return true;
else
return false;
} }
if (multiline && c == '\n')
was_newlines = true;
if (multiline && c == '\0')
fatal (ERR_UNCLOSED);
consume_char ();
}
} }
token token
@@ -604,11 +640,11 @@ lexer_next_token ()
char c = LA (0); char c = LA (0);
if (saved_token.type != TOK_EOF) if (saved_token.type != TOK_EOF)
{ {
token res = saved_token; token res = saved_token;
saved_token.type = TOK_EOF; saved_token.type = TOK_EOF;
return res; return res;
} }
assert (token_start == NULL); assert (token_start == NULL);
@@ -619,98 +655,108 @@ lexer_next_token ()
return parse_number (); return parse_number ();
if (c == '\n') if (c == '\n')
{
consume_char ();
return (token)
{ {
consume_char (); .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 ();
if (isspace (c)) if (isspace (c))
{ {
grobble_whitespaces (); grobble_whitespaces ();
return lexer_next_token (); return lexer_next_token ();
} }
if (c == '/' && LA (1) == '*') if (c == '/' && LA (1) == '*')
{
if (replace_comment_by_newline ())
return (token)
{ {
if (replace_comment_by_newline ()) .type = TOK_NEWLINE, .data.none = NULL
return (token) { .type = TOK_NEWLINE, .data.none = NULL }; };
else else
return lexer_next_token (); return lexer_next_token ();
} }
if (c == '/' && LA (1) == '/') if (c == '/' && LA (1) == '/')
{ {
replace_comment_by_newline (); replace_comment_by_newline ();
return lexer_next_token (); return lexer_next_token ();
} }
switch (c) switch (c)
{ {
case '{': RETURN_PUNC (TOK_OPEN_BRACE); case '{': RETURN_PUNC (TOK_OPEN_BRACE);
case '}': RETURN_PUNC (TOK_CLOSE_BRACE); case '}': RETURN_PUNC (TOK_CLOSE_BRACE);
case '(': RETURN_PUNC (TOK_OPEN_PAREN); case '(': RETURN_PUNC (TOK_OPEN_PAREN);
case ')': RETURN_PUNC (TOK_CLOSE_PAREN); case ')': RETURN_PUNC (TOK_CLOSE_PAREN);
case '[': RETURN_PUNC (TOK_OPEN_SQUARE); case '[': RETURN_PUNC (TOK_OPEN_SQUARE);
case ']': RETURN_PUNC (TOK_CLOSE_SQUARE); case ']': RETURN_PUNC (TOK_CLOSE_SQUARE);
case '.': RETURN_PUNC (TOK_DOT); case '.': RETURN_PUNC (TOK_DOT);
case ';': RETURN_PUNC (TOK_SEMICOLON); case ';': RETURN_PUNC (TOK_SEMICOLON);
case ',': RETURN_PUNC (TOK_COMMA); case ',': RETURN_PUNC (TOK_COMMA);
case '~': RETURN_PUNC (TOK_COMPL); case '~': RETURN_PUNC (TOK_COMPL);
case ':': RETURN_PUNC (TOK_COLON); case ':': RETURN_PUNC (TOK_COLON);
case '?': RETURN_PUNC (TOK_QUERY); case '?': RETURN_PUNC (TOK_QUERY);
case '*': IF_LA_IS ('=', TOK_MULT_EQ, TOK_MULT); case '*': IF_LA_IS ('=', TOK_MULT_EQ, TOK_MULT);
case '/': IF_LA_IS ('=', TOK_DIV_EQ, TOK_DIV); case '/': IF_LA_IS ('=', TOK_DIV_EQ, TOK_DIV);
case '^': IF_LA_IS ('=', TOK_XOR_EQ, TOK_XOR); case '^': IF_LA_IS ('=', TOK_XOR_EQ, TOK_XOR);
case '%': IF_LA_IS ('=', TOK_MOD_EQ, TOK_MOD); case '%': IF_LA_IS ('=', TOK_MOD_EQ, TOK_MOD);
case '+': IF_LA_IS_OR ('+', TOK_DOUBLE_PLUS, '=', TOK_PLUS_EQ, TOK_PLUS); case '+': IF_LA_IS_OR ('+', TOK_DOUBLE_PLUS, '=', TOK_PLUS_EQ, TOK_PLUS);
case '-': IF_LA_IS_OR ('-', TOK_DOUBLE_MINUS, '=', TOK_MINUS_EQ, TOK_MINUS); case '-': IF_LA_IS_OR ('-', TOK_DOUBLE_MINUS, '=', TOK_MINUS_EQ, TOK_MINUS);
case '&': IF_LA_IS_OR ('&', TOK_DOUBLE_AND, '=', TOK_AND_EQ, TOK_AND); case '&': IF_LA_IS_OR ('&', TOK_DOUBLE_AND, '=', TOK_AND_EQ, TOK_AND);
case '|': IF_LA_IS_OR ('|', TOK_DOUBLE_OR, '=', TOK_OR_EQ, TOK_OR); case '|': IF_LA_IS_OR ('|', TOK_DOUBLE_OR, '=', TOK_OR_EQ, TOK_OR);
case '<': case '<':
switch (LA (1)) switch (LA (1))
{ {
case '<': IF_LA_N_IS ('=', TOK_LSHIFT_EQ, TOK_LSHIFT, 2); case '<': IF_LA_N_IS ('=', TOK_LSHIFT_EQ, TOK_LSHIFT, 2);
case '=': RETURN_PUNC_EX (TOK_LESS_EQ, 2); case '=': RETURN_PUNC_EX (TOK_LESS_EQ, 2);
default: RETURN_PUNC (TOK_LESS); default: RETURN_PUNC (TOK_LESS);
} }
case '>':
switch (LA (1))
{
case '>': case '>':
switch (LA (1)) switch (LA (2))
{ {
case '>': case '>': IF_LA_N_IS ('=', TOK_RSHIFT_EX_EQ, TOK_RSHIFT_EX, 3);
switch (LA (2)) case '=': RETURN_PUNC_EX (TOK_RSHIFT_EQ, 3);
{ default: RETURN_PUNC_EX (TOK_RSHIFT, 2);
case '>': IF_LA_N_IS ('=', TOK_RSHIFT_EX_EQ, TOK_RSHIFT_EX, 3);
case '=': RETURN_PUNC_EX (TOK_RSHIFT_EQ, 3);
default: RETURN_PUNC_EX (TOK_RSHIFT, 2);
}
case '=': RETURN_PUNC_EX (TOK_GREATER_EQ, 2);
default: RETURN_PUNC (TOK_GREATER);
} }
case '=': RETURN_PUNC_EX (TOK_GREATER_EQ, 2);
default: RETURN_PUNC (TOK_GREATER);
}
case '=': case '=':
if (LA (1) == '=') if (LA (1) == '=')
IF_LA_N_IS ('=', TOK_TRIPLE_EQ, TOK_DOUBLE_EQ, 2); IF_LA_N_IS ('=', TOK_TRIPLE_EQ, TOK_DOUBLE_EQ, 2);
else else
RETURN_PUNC (TOK_EQ); RETURN_PUNC (TOK_EQ);
case '!': case '!':
if (LA (1) == '=') if (LA (1) == '=')
IF_LA_N_IS ('=', TOK_NOT_DOUBLE_EQ, TOK_NOT_EQ, 2); IF_LA_N_IS ('=', TOK_NOT_DOUBLE_EQ, TOK_NOT_EQ, 2);
else else
RETURN_PUNC (TOK_NOT); RETURN_PUNC (TOK_NOT);
default: default:
unreachable (); unreachable ();
} }
fatal (ERR_NON_CHAR); fatal (ERR_NON_CHAR);
} }
@@ -725,11 +771,11 @@ lexer_next_token ()
if (tok.type == TOK_NEWLINE) if (tok.type == TOK_NEWLINE)
return tok; return tok;
if (tok.type == TOK_CLOSE_BRACE) if (tok.type == TOK_CLOSE_BRACE)
{ {
if (i == 300) if (i == 300)
fprintf (lexer_debug_log, "lexer_next_token(%d): type=0x%x, data=%p\n", i, tok.type, tok.data.none); fprintf (lexer_debug_log, "lexer_next_token(%d): type=0x%x, data=%p\n", i, tok.type, tok.data.none);
i++; i++;
} }
return tok; return tok;
} }
#endif #endif
@@ -737,10 +783,10 @@ lexer_next_token ()
void void
lexer_save_token (token tok) lexer_save_token (token tok)
{ {
#ifdef DEBUG #ifdef DEBUG
if (tok.type == TOK_CLOSE_BRACE) 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); 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; saved_token = tok;
} }
+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"