ps2: final touches
This commit is contained in:
+10
-4
@@ -44,7 +44,8 @@ global_list :
|
||||
| global_list global { $$ = append_to_list_node($1, $2); }
|
||||
;
|
||||
global :
|
||||
global_variable_declaration { $$ = $1; }
|
||||
function { $$ = $1; }
|
||||
| global_variable_declaration { $$ = $1; }
|
||||
;
|
||||
global_variable_declaration :
|
||||
VAR global_variable_list { $$ = node_create(GLOBAL_VARIABLE_DECLARATION, 1, $2); }
|
||||
@@ -55,6 +56,7 @@ global_variable_list :
|
||||
;
|
||||
global_variable :
|
||||
identifier { $$ = $1; }
|
||||
| array_indexing { $$ = $1; }
|
||||
;
|
||||
array_indexing :
|
||||
identifier '[' expression ']' { $$ = node_create(ARRAY_INDEXING, 2, $1, $3); }
|
||||
@@ -133,7 +135,7 @@ while_statement :
|
||||
;
|
||||
expression :
|
||||
expression '?' expression ':' expression {
|
||||
$$ = node_create(OPERATOR, 3, $1, $2, $3);
|
||||
$$ = node_create(OPERATOR, 3, $1, $3, $5);
|
||||
$$->data.operator = "?:";
|
||||
}
|
||||
| expression OR expression {
|
||||
@@ -192,7 +194,7 @@ expression :
|
||||
$$ = node_create(OPERATOR, 1, $2);
|
||||
$$->data.operator = "!";
|
||||
} %prec UNARY_OPERATORS
|
||||
| '(' expression ')' { $$ = $1; }
|
||||
| '(' expression ')' { $$ = $2; }
|
||||
| number { $$ = $1; }
|
||||
| identifier { $$ = $1; }
|
||||
| array_indexing { $$ = $1; }
|
||||
@@ -221,7 +223,11 @@ identifier :
|
||||
number :
|
||||
NUMBER_TOKEN {
|
||||
$$ = node_create(NUMBER_LITERAL, 0);
|
||||
$$->data.number_literal = strtol(yytext, &yytext[0] + strlen(yytext), 10);
|
||||
char *t;
|
||||
$$->data.number_literal = strtol(yytext, &t, 10);
|
||||
if (yytext == t) {
|
||||
fprintf(stderr, "failed to parse number literal: %s", yytext);
|
||||
}
|
||||
}
|
||||
;
|
||||
string :
|
||||
|
||||
+3
-4
@@ -15,14 +15,13 @@
|
||||
WHITESPACE [ \v\t\n\r]
|
||||
COMMENT \/\/[^\n]+
|
||||
QUOTED \"([^\"\n]|\\\")*\"
|
||||
NUMERIC ^[0-9]+$
|
||||
BINDING ^[a-zA-Z]+[a-zA-Z0-9_]*$
|
||||
NUMERIC [0-9]+
|
||||
BINDING [a-zA-Z][a-zA-Z_0-9]*
|
||||
|
||||
%%
|
||||
{COMMENT} { /* Eliminate comments */ }
|
||||
{WHITESPACE}+ { /* Eliminate whitespace */ }
|
||||
{COMMENT} { /* Eliminate comments */ }
|
||||
{QUOTED} { return STRING_TOKEN; }
|
||||
|
||||
func { return FUNC; }
|
||||
var { return VAR; }
|
||||
return { return RETURN; }
|
||||
|
||||
+9
-3
@@ -18,7 +18,7 @@ node_t* node_create(node_type_t type, size_t n_children, ...)
|
||||
node_t *node = malloc(sizeof(node_t));
|
||||
node->type = type;
|
||||
node->n_children = n_children;
|
||||
node->children = malloc(sizeof(node_t) * n_children);
|
||||
node->children = malloc(sizeof(node_t *) * n_children);
|
||||
|
||||
va_list args;
|
||||
va_start(args, n_children);
|
||||
@@ -118,8 +118,14 @@ static void node_print(node_t* node, int nesting)
|
||||
// Frees the memory owned by the given node, but does not touch its children
|
||||
static void node_finalize(node_t* discard)
|
||||
{
|
||||
if (discard->type == IDENTIFIER || discard->type == STRING_LITERAL)
|
||||
free(&discard->data);
|
||||
switch (discard->type) {
|
||||
case IDENTIFIER:
|
||||
free(discard->data.identifier);
|
||||
break;
|
||||
case STRING_LITERAL:
|
||||
free(discard->data.string_literal);
|
||||
break;
|
||||
}
|
||||
free(discard->children);
|
||||
free(discard);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user