Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
m-heim committed Jul 30, 2022
1 parent 88f28cf commit 25f2da2
Show file tree
Hide file tree
Showing 7 changed files with 4,251,268 additions and 24 deletions.
File renamed without changes.
12 changes: 12 additions & 0 deletions programs/simple_test_program.npc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#PROGRAM test
#using std.system.println
int main() {
int x = 3;
long y = 2;
int a = x + y * 3;
if (a > 8) {
println("We are good");
}
return 0;
}
#END
16 changes: 8 additions & 8 deletions src/ir_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ ir_gen_result generate(parser_result parser_out) {
while (1) {
}
}
/*
ir_gen_result *translate(ir_gen_result *i_result, parser_result parser_result,
/*ir_gen_result *translate(ir_gen *ir_gen,
long place) {
ast *tree = parser_result.tree;
ast *tree = ir_gen->parser_result.tree;
switch (tree->n.type) {
case function_n:
three_address_code_add(i_result->code, MAXIMUM_LABEL, funheader,
three_address_code_add(ir_gen.code, MAXIMUM_LABEL, funheader,
address_undefined, UNDEFINED, address_undefined,
UNDEFINED, address_undefined, UNDEFINED);
if (tree->children[2]->n.type == parameter_list_n) {
Expand Down Expand Up @@ -131,8 +130,8 @@ ir_gen_result *translate(ir_gen_result *i_result, parser_result parser_result,
}
}
*/

/*three_address_code_op get_op(token_type type) {
/*
three_address_code_op get_op(token_type type) {
if (type == identifier_token) {
return "identifier_token";
} else if (type == assignment_token) {
Expand Down Expand Up @@ -258,8 +257,9 @@ ir_gen_result *translate(ir_gen_result *i_result, parser_result parser_result,
}
}
*/

/*
long new_var() {
static long current;
return current++;
}
}
*/
11 changes: 9 additions & 2 deletions src/ir_gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ typedef struct v_table {
size_t used;
} v_table;

// The result of the IR generation, it holds the three address code and the
// variable table
typedef struct ir_gen {
parser_result parser_result;
v_table v_table;
three_address_code code;
} ir_gen;

/**
* @brief Holds the output of the intermediate repr. generation, consists of the corresponding three address code and the
*/
typedef struct ir_gen_result {
three_address_code *code;
v_table *table;
Expand Down
32 changes: 19 additions & 13 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ parser_result *parse_program(scanner_result res, int debug) {
if (debug) {
print_tree(parser->tree, 0);
}
return parser_result_make(parser->tree, res.table, type_table);
return parser_result_make(parser->tree, res.table,type_table);
}

void parse_syntax_err(parser *parser, char *err) {
Expand Down Expand Up @@ -278,6 +278,7 @@ void return_statement(parser *parser) {
match_no_append(parser, return_keyword_token);
// should also be able to return statement?
expression(parser);
match_no_append(parser, semicolon_token);

parser->tree = ast_get_parent(parser->tree);
if (parser->debug) {
Expand Down Expand Up @@ -329,6 +330,7 @@ void assignment(parser *parser) {
match_by_class(parser, assign_c);

expression(parser);
match(parser, semicolon_token);
parser->tree = ast_get_parent(parser->tree);
}

Expand Down Expand Up @@ -357,8 +359,10 @@ void statement(parser *parser) {
opening_bracket_token) {
fun_call(parser);
} else if (token_array_get_token_type_class(
parser->arr, parser->position) == type_c) {
declaration(parser);
parser->arr, parser->position + 1) == assign_c) {
assignment(parser);
} else {
parse_syntax_err(parser, "Expected Function call or assignment");
}
} else if (token_array_get_token_type(parser->arr, parser->position) ==
return_keyword_token) {
Expand Down Expand Up @@ -468,8 +472,9 @@ void declaration(parser *parser) {
match(parser, identifier_token);
if (token_array_get_token_type(parser->arr, parser->position) ==
assignment_token) {
match(parser, assignment_token);
match_no_append(parser, assignment_token);
expression(parser);
match_no_append(parser, semicolon_token);
} else {
parse_syntax_err(parser, "expected assignment token");
}
Expand All @@ -495,7 +500,9 @@ void fun_call(parser *parser) {
argument_list(parser);
}
match_no_append(parser, closing_bracket_token);
match_no_append(parser, semicolon_token);
parser->tree = ast_get_parent(parser->tree);
npc_debug_log(parser->debug, "Parsing function call done");
}

void argument(parser *parser) {
Expand Down Expand Up @@ -605,7 +612,6 @@ void block(parser *parser) {
while (token_array_get_token_type(parser->arr, parser->position) !=
closing_c_bracket_token) {
statement(parser);
match_no_append(parser, semicolon_token);
}

match_no_append(parser, closing_c_bracket_token);
Expand Down Expand Up @@ -692,8 +698,8 @@ void print_tree(ast *tree, int depth) {

void match(parser *parser, token_type type) {
if (parser->debug) {
printf("Matching %s at %ld\n", token_type_get_canonial(type),
parser->position);
printf("Matching %s at %ld, got %s\n", token_type_get_canonial(type),
parser->position, token_type_get_canonial(token_array_get_token_type(parser->arr, parser->position)));
}
if (token_array_get_token_type(parser->arr, parser->position) == type) {
ast_append(parser->tree,
Expand All @@ -707,8 +713,8 @@ void match(parser *parser, token_type type) {

void match_no_append(parser *parser, token_type type) {
if (parser->debug) {
printf("Matching %s at %ld\n", token_type_get_canonial(type),
parser->position);
printf("Matching %s at %ld, got %s\n", token_type_get_canonial(type),
parser->position, token_type_get_canonial(token_array_get_token_type(parser->arr, parser->position)));
}
if (token_array_get_token_type(parser->arr, parser->position) == type) {
parser->position++;
Expand All @@ -720,8 +726,8 @@ void match_no_append(parser *parser, token_type type) {

void match_by_class(parser *parser, token_type_class type) {
if (parser->debug) {
printf("Matching %s at %ld\n", token_type_get_class(type),
parser->position);
printf("Matching %s at %ld, got %s\n", token_type_get_class(type),
parser->position, token_type_get_class(token_array_get_token_type_class(parser->arr, parser->position)));
}
if (token_array_get_token_type_class(parser->arr, parser->position) ==
type) {
Expand All @@ -736,8 +742,8 @@ void match_by_class(parser *parser, token_type_class type) {

void match_by_class_no_append(parser *parser, token_type_class type) {
if (parser->debug) {
printf("Matching %s at %ld\n", token_type_get_class(type),
parser->position);
printf("Matching %s at %ld, got %s\n", token_type_get_class(type),
parser->position, token_type_get_class(token_array_get_token_type_class(parser->arr, parser->position)));
}
if (token_array_get_token_type_class(parser->arr, parser->position) ==
type) {
Expand Down
3 changes: 2 additions & 1 deletion src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ scanner_result lex(char *code, int debug, int export_symbol) {
npc_debug_log(debug, "{Lex} - now lexing.");
char outputbuf[100];
size_t token_index = 0;
while (*(code + position) != '\0') {
// is a hack to prevent it from stopping to scanning when we found the #end token
while (state != 0 || *(code + position) != '\0') {
cur = code + position;
token_index = arr->used;
sprintf(outputbuf, "{char=%c|state=%d}", *cur, state);
Expand Down
Loading

0 comments on commit 25f2da2

Please sign in to comment.