This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
generated from dthain/compilerbook-starter-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create test cases for printer * Add printer test in runtest.sh * Add --print option * Add rules for printer and other improvements * Create printer.c * Define semantic values in lex.yy.l * Modify parse() to support parsing consecutive files * Modify parse() to return AST root * Add actions after reduction in grammar.y * Add param_list_create() and param_list_print() * Add expr_create() and expr_print() * Add type_create() and type_print() * Add stmt_create() and stmt_print() * Add decl_create() and decl_print() * Pass file pointers to decode(), scan(), and parse() * Modify parse() to take file pointer * Modify scan() to take file pointer * Modify decode() to take file pointer * Move definition of MAX_STRING_LEN * Add grammar.output to .gitignore
- Loading branch information
Showing
35 changed files
with
1,193 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ bminor | |
lex.yy.c | ||
token.h | ||
grammar.tab.c | ||
grammar.output | ||
|
||
test/**/*.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "decl.h" | ||
#include "type.h" | ||
|
||
struct decl* decl_create(char* name, struct type* type, struct expr* value, struct stmt* code, struct decl* next) | ||
{ | ||
struct decl* d = malloc(sizeof(*d)); | ||
d->name = name; | ||
d->type = type; | ||
d->value = value; | ||
d->code = code; | ||
d->next = next; | ||
return d; | ||
} | ||
|
||
void decl_print(struct decl* d, int indent) | ||
{ | ||
if (!d) return; | ||
int i; | ||
|
||
for (i = 0; i < indent; i++) | ||
printf("\t"); | ||
printf("%s: ", d->name); | ||
type_print(d->type); | ||
|
||
if (d->value) | ||
{ | ||
printf(" = "); | ||
if (d->value->kind == EXPR_LIST) | ||
{ | ||
printf("{"); | ||
expr_print(d->value); | ||
printf("}"); | ||
} | ||
else | ||
{ | ||
expr_print(d->value); | ||
} | ||
} | ||
|
||
if (d->code) | ||
{ | ||
printf(" = {\n"); | ||
stmt_print(d->code, indent + 1); | ||
for (i = 0; i < indent; i++) | ||
printf("\t"); | ||
printf("}"); | ||
} | ||
else | ||
{ | ||
// Functions don't end with semicolon | ||
printf(";"); | ||
} | ||
|
||
printf("\n"); | ||
decl_print(d->next, indent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,21 @@ | ||
|
||
#ifndef DECL_H | ||
#define DECL_H | ||
|
||
#include "type.h" | ||
#include "stmt.h" | ||
#include "expr.h" | ||
#include <stdio.h> | ||
#include "symbol.h" | ||
|
||
struct decl { | ||
char *name; | ||
struct type *type; | ||
struct expr *value; | ||
struct stmt *code; | ||
struct symbol *symbol; | ||
struct decl *next; | ||
struct decl | ||
{ | ||
char* name; | ||
struct type* type; | ||
struct expr* value; | ||
struct stmt* code; | ||
struct symbol* symbol; | ||
struct decl* next; | ||
}; | ||
|
||
struct decl * decl_create( char *name, struct type *type, struct expr *value, struct stmt *code, struct decl *next ); | ||
void decl_print( struct decl *d, int indent ); | ||
struct decl* decl_create(char* name, struct type* type, struct expr* value, struct stmt* code, struct decl* next); | ||
void decl_print(struct decl* d, int indent); | ||
|
||
#endif | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
#ifndef ENCODER_H | ||
#define ENCODER_H | ||
|
||
#define MAX_STRING_LEN 255 | ||
|
||
int string_decode(const char* es, char* s); | ||
|
||
int string_encode(const char* s, char* es); | ||
|
||
int decode(const char* filename); | ||
int decode(FILE *fp); | ||
|
||
#endif |
Oops, something went wrong.