Compare commits

..

No commits in common. "fe49453d0a961a97f26bbc7a314e2d326e37e99c" and "6856407f3843f5a11da6f3ad8c9c4d1dcf4ae5ad" have entirely different histories.

8 changed files with 237 additions and 455 deletions

View File

@ -10,16 +10,8 @@ typedef struct n {
struct n **children; struct n **children;
} node_t; } node_t;
/**Export the initializer function, it is needed by the parser // Export the initializer function, it is needed by the parser
* @param *nd node to initialize void node_init (
* @param type type of node (see nodetype.h)
* @param *data associated data. Declared void to allow any type
* @param n_children number of children
* @param ... variable argument list of child nodes (node_t *)
*
* @return Pointer to the initialized node
* */
node_t* node_init (
node_t *nd, node_index_t type, void *data, uint64_t n_children, ... node_t *nd, node_index_t type, void *data, uint64_t n_children, ...
); );

View File

@ -25,7 +25,6 @@ int yyerror ( const char *error );
/* These are defined in the parser generated by bison */ /* These are defined in the parser generated by bison */
extern int yylineno; extern int yylineno;
extern int yylex ( void ); extern int yylex ( void );
extern int yylex_destroy( void );
extern char yytext[]; extern char yytext[];
/* Global state */ /* Global state */

View File

@ -1,303 +1,176 @@
%{ %{
#include <vslc.h> #include <vslc.h>
#define NODE(type, data, n_children, children...) node_init(malloc(sizeof(node_t)), type, data, n_children, ##children) #define N0C(n,t,d) do { \
node_init ( n = malloc(sizeof(node_t)), t, d, 0 ); \
} while ( false )
#define N1C(n,t,d,a) do { \
node_init ( n = malloc(sizeof(node_t)), t, d, 1, a ); \
} while ( false )
#define N2C(n,t,d,a,b) do { \
node_init ( n = malloc(sizeof(node_t)), t, d, 2, a, b ); \
} while ( false )
#define N3C(n,t,d,a,b,c) do { \
node_init ( n = malloc(sizeof(node_t)), t, d, 3, a, b, c ); \
} while ( false )
%} %}
%define api.value.type {node_t} %left '|'
%token FUNC PRINT RETURN CONTINUE IF THEN ELSE WHILE DO OPENBLOCK CLOSEBLOCK %left '^'
%token VAR NUMBER IDENTIFIER STRING %left '&'
%left '|' '&' '^'
%left '+' '-' %left '+' '-'
%left '*' '/' %left '*' '/'
%nonassoc UMINUS %nonassoc UMINUS
%right '~' %right '~'
//%expect 1 %expect 1
%nonassoc IF THEN %token FUNC PRINT RETURN CONTINUE IF THEN ELSE WHILE DO OPENBLOCK CLOSEBLOCK
%nonassoc ELSE %token VAR NUMBER IDENTIFIER STRING
/* Tried fixing vscode complaining about the type for the non-terminals, didn't work
%union {
node_t* node;
}
%type <node> global_list global
%type <node> statement_list print_list expression_list variable_list argument_list parameter_list declaration_list
%type <node> function statement block
%type <node> assignment_statement return_statement print_statement null_statement if_statement while_statement
%type <node> relation expression declaration print_item identifier number string
*/
%% %%
program :
program: global_list { N1C ( root, PROGRAM, NULL, $1 ); }
global_list { ;
root = NODE(PROGRAM, NULL, 1, $1); global_list :
} global { N1C ( $$, GLOBAL_LIST, NULL, $1 ); }
; | global_list global { N2C ( $$, GLOBAL_LIST, NULL, $1, $2 ); }
;
global_list:
global {
$$ = NODE(GLOBAL_LIST, NULL, 1, $1);
}
| global_list global {
$$ = NODE(GLOBAL_LIST, NULL, 2, $1, $2);
}
;
global: global:
function { function { N1C ( $$, GLOBAL, NULL, $1 ); }
$$ = NODE(GLOBAL, NULL, 1, $1); | declaration { N1C ( $$, GLOBAL, NULL, $1 ); }
} ;
| declaration { statement_list :
$$ = NODE(GLOBAL, NULL, 1, $1); statement { N1C ( $$, STATEMENT_LIST, NULL, $1 ); }
} | statement_list statement { N2C ( $$, STATEMENT_LIST, NULL, $1, $2 ); }
; ;
print_list :
statement_list: print_item { N1C ( $$, PRINT_LIST, NULL, $1 ); }
statement { | print_list ',' print_item { N2C ( $$, PRINT_LIST, NULL, $1, $3 ); }
$$ = NODE(STATEMENT_LIST, NULL, 1, $1); ;
} expression_list :
| statement_list statement { expression { N1C ( $$, EXPRESSION_LIST, NULL, $1 ); }
$$ = NODE(STATEMENT_LIST, NULL, 2, $1, $2); | expression_list ',' expression { N2C($$, EXPRESSION_LIST, NULL, $1, $3); }
} ;
; variable_list :
identifier { N1C ( $$, VARIABLE_LIST, NULL, $1 ); }
print_list: | variable_list ',' identifier { N2C ( $$, VARIABLE_LIST, NULL, $1, $3 ); }
print_item { ;
$$ = NODE(PRINT_LIST, NULL, 1, $1); argument_list :
} expression_list { N1C ( $$, ARGUMENT_LIST, NULL, $1 ); }
| print_list ',' print_item { | /* epsilon */ { $$ = NULL; }
$$ = NODE(PRINT_LIST, NULL, 2, $1, $3); ;
} parameter_list :
; variable_list { N1C ( $$, PARAMETER_LIST, NULL, $1 ); }
| /* epsilon */ { $$ = NULL; }
expression_list: ;
expression { declaration_list :
$$ = NODE(EXPRESSION_LIST, NULL, 1, $1); declaration { N1C ( $$, DECLARATION_LIST, NULL, $1 ); }
} | declaration_list declaration { N2C ($$, DECLARATION_LIST, NULL, $1, $2); }
| expression_list ',' expression { ;
$$ = NODE(EXPRESSION_LIST, NULL, 2, $1, $3); function :
} FUNC identifier '(' parameter_list ')' statement
; { N3C ( $$, FUNCTION, NULL, $2, $4, $6 ); }
;
variable_list: statement :
identifier { assignment_statement { N1C ( $$, STATEMENT, NULL, $1 ); }
$$ = NODE(VARIABLE_LIST, NULL, 1, $1); | return_statement { N1C ( $$, STATEMENT, NULL, $1 ); }
} | print_statement { N1C ( $$, STATEMENT, NULL, $1 ); }
| variable_list ',' identifier { | if_statement { N1C ( $$, STATEMENT, NULL, $1 ); }
$$ = NODE(VARIABLE_LIST, NULL, 2, $1, $3); | while_statement { N1C ( $$, STATEMENT, NULL, $1 ); }
} | null_statement { N1C ( $$, STATEMENT, NULL, $1 ); }
; | block { N1C ( $$, STATEMENT, NULL, $1 ); }
;
argument_list: block :
expression_list { OPENBLOCK declaration_list statement_list CLOSEBLOCK
$$ = NODE(ARGUMENT_LIST, NULL, 1, $1); { N2C ($$, BLOCK, NULL, $2, $3); }
} | OPENBLOCK statement_list CLOSEBLOCK { N1C ($$, BLOCK, NULL, $2 ); }
| /* epsilon */ { ;
$$ = NODE(ARGUMENT_LIST, NULL, 0); assignment_statement :
} identifier ':' '=' expression
; { N2C ( $$, ASSIGNMENT_STATEMENT, NULL, $1, $4 ); }
| identifier '+' '=' expression
parameter_list: { N2C ( $$, ADD_STATEMENT, NULL, $1, $4 ); }
variable_list { | identifier '-' '=' expression
$$ = NODE(PARAMETER_LIST, NULL, 1, $1); { N2C ( $$, SUBTRACT_STATEMENT, NULL, $1, $4 ); }
} | identifier '*' '=' expression
| /* epsilon */ { { N2C ( $$, MULTIPLY_STATEMENT, NULL, $1, $4 ); }
$$ = NODE(PARAMETER_LIST, NULL, 0); | identifier '/' '=' expression
} { N2C ( $$, DIVIDE_STATEMENT, NULL, $1, $4 ); }
; ;
return_statement :
declaration_list: RETURN expression
declaration { { N1C ( $$, RETURN_STATEMENT, NULL, $2 ); }
$$ = NODE(DECLARATION_LIST, NULL, 1, $1); ;
} print_statement :
| declaration_list declaration { PRINT print_list
$$ = NODE(DECLARATION_LIST, NULL, 2, $1, $2); { N1C ( $$, PRINT_STATEMENT, NULL, $2 ); }
} ;
; null_statement :
CONTINUE
function: { N0C ( $$, NULL_STATEMENT, NULL ); }
FUNC identifier '(' parameter_list ')' statement { ;
$$ = NODE(FUNCTION, NULL, 3, $2, $4, $6); if_statement :
} IF relation THEN statement
; { N2C ( $$, IF_STATEMENT, NULL, $2, $4 ); }
| IF relation THEN statement ELSE statement
statement: { N3C ( $$, IF_STATEMENT, NULL, $2, $4, $6 ); }
assignment_statement { ;
$$ = NODE(STATEMENT, NULL, 1, $1); while_statement :
} WHILE relation DO statement
| return_statement { { N2C ( $$, WHILE_STATEMENT, NULL, $2, $4 ); }
$$ = NODE(STATEMENT, NULL, 1, $1); ;
}
| print_statement {
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| if_statement {
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| while_statement {
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| null_statement {
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| block {
$$ = NODE(STATEMENT, NULL, 1, $1);
}
;
block:
OPENBLOCK declaration_list statement_list CLOSEBLOCK {
$$ = NODE(BLOCK, NULL, 2, $2, $3);
}
| OPENBLOCK statement_list CLOSEBLOCK {
$$ = NODE(BLOCK, NULL, 1, $2);
}
;
assignment_statement:
identifier ':' '=' expression {
$$ = NODE(ASSIGNMENT_STATEMENT, NULL, 2, $1, $4);
}
| identifier '+' '=' expression {
$$ = NODE(ADD_STATEMENT, NULL, 2, $1, $4);
}
| identifier '-' '=' expression {
$$ = NODE(SUBTRACT_STATEMENT, NULL, 2, $1, $4);
}
| identifier '*' '=' expression {
$$ = NODE(MULTIPLY_STATEMENT, NULL, 2, $1, $4);
}
| identifier '/' '=' expression {
$$ = NODE(DIVIDE_STATEMENT, NULL, 2, $1, $4);
}
;
return_statement:
RETURN expression {
$$ = NODE(RETURN_STATEMENT, NULL, 1, $2);
}
;
print_statement:
PRINT print_list {
$$ = NODE(PRINT_STATEMENT, NULL, 1, $2);
}
;
null_statement:
CONTINUE {
$$ = NODE(NULL_STATEMENT, NULL, 0);
}
;
if_statement:
IF relation THEN statement {
$$ = NODE(IF_STATEMENT, NULL, 2, $2, $4);
}
| IF relation THEN statement ELSE statement {
$$ = NODE(IF_STATEMENT, NULL, 3, $2, $4, $6);
}
;
while_statement:
WHILE relation DO statement {
$$ = NODE(WHILE_STATEMENT, NULL, 2, $2, $4);
}
;
relation: relation:
expression '=' expression { expression '=' expression
$$ = NODE(RELATION, strdup("="), 2, $1, $3); { N2C ( $$, RELATION, strdup("="), $1, $3 ); }
} | expression '<' expression
| expression '<' expression { { N2C ( $$, RELATION, strdup("<"), $1, $3 ); }
$$ = NODE(RELATION, strdup("<"), 2, $1, $3); | expression '>' expression
} { N2C ( $$, RELATION, strdup(">"), $1, $3 ); }
| expression '>' expression { ;
$$ = NODE(RELATION, strdup(">"), 2, $1, $3); expression :
} expression '|' expression
; { N2C ( $$, EXPRESSION, strdup("|"), $1, $3 ); }
| expression '^' expression
expression: { N2C ( $$, EXPRESSION, strdup("^"), $1, $3 ); }
expression '|' expression { | expression '&' expression
$$ = NODE(EXPRESSION, strdup("|"), 2, $1, $3); { N2C ( $$, EXPRESSION, strdup("&"), $1, $3 ); }
} | expression '+' expression
| expression '^' expression { { N2C ( $$, EXPRESSION, strdup("+"), $1, $3 ); }
$$ = NODE(EXPRESSION, strdup("^"), 2, $1, $3); | expression '-' expression
} { N2C ( $$, EXPRESSION, strdup("-"), $1, $3 ); }
| expression '&' expression { | expression '*' expression
$$ = NODE(EXPRESSION, strdup("&"), 2, $1, $3); { N2C ( $$, EXPRESSION, strdup("*"), $1, $3 ); }
} | expression '/' expression
| expression '+' expression { { N2C ( $$, EXPRESSION, strdup("/"), $1, $3 ); }
$$ = NODE(EXPRESSION, strdup("+"), 2, $1, $3); | '-' expression %prec UMINUS
} { N1C ( $$, EXPRESSION, strdup("-"), $2 ); }
| expression '-' expression { | '~' expression %prec UMINUS
$$ = NODE(EXPRESSION, strdup("-"), 2, $1, $3); { N1C ( $$, EXPRESSION, strdup("~"), $2 ); }
} | '(' expression ')' { $$ = $2; }
| expression '*' expression { | number { N1C ( $$, EXPRESSION, NULL, $1 ); }
$$ = NODE(EXPRESSION, strdup("*"), 2, $1, $3); | identifier
} { N1C ( $$, EXPRESSION, NULL, $1 ); }
| expression '/' expression { | identifier '(' argument_list ')'
$$ = NODE(EXPRESSION, strdup("/"), 2, $1, $3); { N2C ( $$, EXPRESSION, NULL, $1, $3 ); }
} ;
| '-' expression %prec UMINUS { declaration :
$$ = NODE(EXPRESSION, strdup("-"), 1, $2); VAR variable_list { N1C ( $$, DECLARATION, NULL, $2 ); }
} ;
| '~' expression { print_item :
$$ = NODE(EXPRESSION, strdup("~"), 1, $2); expression
} { N1C ( $$, PRINT_ITEM, NULL, $1 ); }
| '(' expression ')' { | string
$$ = NODE(EXPRESSION, /*NULL*/ strdup("group"), 1, $2); { N1C ( $$, PRINT_ITEM, NULL, $1 ); }
} ;
| number { identifier: IDENTIFIER { N0C($$, IDENTIFIER_DATA, strdup(yytext) ); }
$$ = NODE(EXPRESSION, /*NULL*/ strdup("number"), 1, $1); number: NUMBER
} {
| identifier { int64_t *value = malloc ( sizeof(int64_t) );
$$ = NODE(EXPRESSION, /*NULL*/ strdup("identifier"), 1, $1); *value = strtol ( yytext, NULL, 10 );
} N0C($$, NUMBER_DATA, value );
| identifier '(' argument_list ')' { }
$$ = NODE(EXPRESSION, /*NULL*/ strdup("function_call"), 2, $1, $3); string: STRING { N0C($$, STRING_DATA, strdup(yytext) ); }
}
;
declaration:
VAR variable_list {
$$ = NODE(DECLARATION, NULL, 1, $2);
}
;
print_item:
expression {
$$ = NODE(PRINT_ITEM, NULL, 1, $1);
}
| string {
$$ = NODE(PRINT_ITEM, NULL, 1, $1);
}
;
identifier:
IDENTIFIER {
$$ = NODE(IDENTIFIER_DATA, strdup(yytext), 0); // Zero children
}
;
number:
NUMBER {
uint64_t* p_number = malloc(sizeof(uint64_t));
*p_number = strtol(yytext, NULL, 10);
$$ = NODE(NUMBER_DATA, p_number, 0); // Zero children
}
;
string:
STRING {
$$ = NODE(STRING_DATA, strdup(yytext), 0); // Zero children
}
;
%% %%
int int

View File

@ -40,8 +40,7 @@ print_syntax_tree ( void )
} }
// Changed so it returns the pointer to the new node, can be used as before, but makes the parser file cleaner void
node_t*
node_init (node_t *nd, node_index_t type, void *data, uint64_t n_children, ...) node_init (node_t *nd, node_index_t type, void *data, uint64_t n_children, ...)
{ {
va_list child_list; va_list child_list;
@ -56,8 +55,6 @@ node_init (node_t *nd, node_index_t type, void *data, uint64_t n_children, ...)
for ( uint64_t i=0; i<n_children; i++ ) for ( uint64_t i=0; i<n_children; i++ )
nd->children[i] = va_arg ( child_list, node_t * ); nd->children[i] = va_arg ( child_list, node_t * );
va_end ( child_list ); va_end ( child_list );
return nd;
} }
@ -153,161 +150,83 @@ destroy_subtree ( node_t *discard )
} }
} }
static void
prune_children(node_t **simplified, node_t *root)
{
if (!root)
return;
node_t *result = root;
switch (root->type)
{
case GLOBAL:
//case ARGUMENT_LIST:
//case PARAMETER_LIST:
case STATEMENT:
case PRINT_ITEM:
case PRINT_STATEMENT:
result = root->children[0];
// The print_statement only contains a print_list, still need a print_statement.
if (root->type == PRINT_STATEMENT)
result->type = PRINT_STATEMENT;
node_finalize(root);
break;
}
*simplified = result;
}
static void
resolve_constant_expressions(node_t **simplified, node_t *root)
{
if (!root)
return;
if (root->type != EXPRESSION)
return;
node_t *result = root;
switch (root->n_children)
{
case 1:
result = root->children[0];
if (root->data &&
result->type == NUMBER_DATA &&
result->data)
{
switch (*((char*)root->data))
{
case '-':
*((int64_t*)result->data) *= -1;
break;
case '~':
*((int64_t*)result->data) = ~*((int64_t*)result->data);
break;
}
}
node_finalize(root);
break;
case 2:
// Both children needs to be numbers to resolve constants
if (root->children[0]->type == NUMBER_DATA &&
root->children[1]->type == NUMBER_DATA)
{
// Check if children does not contain null pointers
if (!root->children[0]->data)
break;
if (!root->children[1]->data)
break;
// Check if data field is not null pointer
if (!root->data)
break;
result = root->children[0];
int64_t
*lhs = result->data,
*rhs = root->children[1]->data;
switch (*(char*)root->data)
{
case '|': *lhs |= *rhs; break;
case '^': *lhs ^= *rhs; break;
case '&': *lhs &= *rhs; break;
case '+': *lhs += *rhs; break;
case '-': *lhs -= *rhs; break;
case '*': *lhs *= *rhs; break;
case '/': *lhs /= *rhs; break;
}
node_finalize(root->children[1]);
node_finalize(root);
}
break;
}
*simplified = result;
}
static void
flatten(node_t **simplified, node_t *root)
{
/* This will flatten left-expanded lists */
if (!root)
return;
node_t **new_children, *result = root;
switch (root->type)
{
case GLOBAL_LIST:
case STATEMENT_LIST:
case PRINT_LIST:
case EXPRESSION_LIST:
case VARIABLE_LIST:
case DECLARATION_LIST:
// Check if node have more than two children
if (root->n_children < 2)
break;
result = root->children[0];
result->n_children++;
// Realloc the array of children to the new size
if (!(new_children = realloc(result->children, result->n_children * sizeof(node_t*))))
break;
// if successs, insert the new array
result->children = new_children;
// Insert child at the end
result->children[result->n_children - 1] = root->children[1];
node_finalize(root);
break;
}
*simplified = result;
}
static void static void
simplify_tree ( node_t **simplified, node_t *root ) simplify_tree ( node_t **simplified, node_t *root )
{ {
if (!root) if ( root == NULL )
return; return;
for (int i = 0; i < root->n_children; i++) /* Simplify subtrees before examining this node */
simplify_tree(&root->children[i], root->children[i]); for ( uint64_t i=0; i<root->n_children; i++ )
simplify_tree ( &root->children[i], root->children[i] );
prune_children(&root, root); node_t *discard, *result = root;
resolve_constant_expressions(&root, root); switch ( root->type )
flatten(&root, root); {
/* Structures of purely syntactic function */
*simplified = root; case PARAMETER_LIST: case ARGUMENT_LIST:
case STATEMENT: case PRINT_ITEM: case GLOBAL:
result = root->children[0];
node_finalize ( root );
break;
case PRINT_STATEMENT:
result = root->children[0];
result->type = PRINT_STATEMENT;
node_finalize(root);
/* Flatten lists:
* Take left child, append right child, substitute left for root.
*/
case STATEMENT_LIST: case DECLARATION_LIST: case GLOBAL_LIST:
case PRINT_LIST: case EXPRESSION_LIST: case VARIABLE_LIST:
if ( root->n_children >= 2 )
{
result = root->children[0];
result->n_children += 1;
result->children = realloc (
result->children, result->n_children * sizeof(node_t *)
);
result->children[result->n_children-1] = root->children[1];
node_finalize ( root );
}
break;
case EXPRESSION:
switch ( root->n_children )
{
case 1:
if ( root->children[0]->type == NUMBER_DATA )
{
result = root->children[0];
if ( root->data != NULL )
*((int64_t *)result->data) *= -1;
node_finalize (root);
}
else if ( root->data == NULL )
{
result = root->children[0];
node_finalize (root);
}
break;
case 2:
if ( root->children[0]->type == NUMBER_DATA &&
root->children[1]->type == NUMBER_DATA
) {
result = root->children[0];
int64_t
*x = result->data,
*y = root->children[1]->data;
switch ( *((char *)root->data) )
{
case '+': *x += *y; break;
case '-': *x -= *y; break;
case '*': *x *= *y; break;
case '/': *x /= *y; break;
}
node_finalize ( root->children[1] );
node_finalize ( root );
}
break;
}
}
*simplified = result;
} }

View File

@ -28,7 +28,6 @@ main ( int argc, char **argv )
options ( argc, argv ); options ( argc, argv );
yyparse(); // Generated from grammar/bison, constructs syntax tree yyparse(); // Generated from grammar/bison, constructs syntax tree
yylex_destroy(); // Free heap used by flex
if ( print_full_tree ) if ( print_full_tree )
print_syntax_tree (); print_syntax_tree ();

View File

@ -9,8 +9,8 @@ ps2: $(PS2_EXAMPLES)
ps3: $(PS3_EXAMPLES) ps3: $(PS3_EXAMPLES)
ps4: $(PS4_EXAMPLES) ps4: $(PS4_EXAMPLES)
%.ast: %.vsl clean %.ast: %.vsl
$(VSLC) -t -T -s < $^ > $@ $(VSLC) -t < $^ > $@
clean: clean:
-rm -r */*.ast -rm -r */*.ast

View File

@ -12,5 +12,5 @@ end
func main() begin func main() begin
var n, o, p, q, r, s, t, u, v, w var n, o, p, q, r, s, t, u, v, w
n := 5 n := 5
n += my_func(1, 2, 3, 5, 8, 13, 21, w) n += my_func(1, 2, 3, 5, 8, 13, 21, 34)
end end