Browse Source

Added UASTRootNode

- This'll let me include more information later, like type info, etc.
remotes/origin/HEAD
Seth Stubbs 5 months ago
parent
commit
bad7dec335
  1. 4
      src/main.c
  2. 6
      src/uasm.c
  3. 2
      src/uasm.h
  4. 9
      src/uparse.c
  5. 8
      src/uparse.h

4
src/main.c

@ -2,7 +2,7 @@
#include "uasm.h"
int main() {
UASTNode *tree = UP_parseSource(
UASTRootNode *tree = UP_parseSource(
"int a;\n"
"int b;"
"a = 8;\n"
@ -13,6 +13,6 @@ int main() {
UA_genTal(tree, fopen("bin/out.tal", "w"));
/* clean up */
UP_freeTree(tree);
UP_freeTree((UASTNode*)tree);
return 0;
}

6
src/uasm.c

@ -352,7 +352,7 @@ void compileAST(UCompState *state, UASTNode *node) {
}
}
void UA_genTal(UASTNode *tree, FILE *out) {
void UA_genTal(UASTRootNode *tree, FILE *out) {
UCompState state;
state.sCount = 0;
state.pushed = 0;
@ -362,7 +362,9 @@ void UA_genTal(UASTNode *tree, FILE *out) {
fwrite(preamble, sizeof(preamble)-1, 1, out);
/* now parse the whole AST */
compileAST(&state, tree);
pushScope(&state, &tree->scope);
compileAST(&state, tree->_node.left);
popScope(&state);
/* finally, write the postamble */
fwrite(postamble, sizeof(postamble)-1, 1, out);

2
src/uasm.h

@ -13,6 +13,6 @@
#include <stdio.h>
/* takes a syntax tree and spits out the generated asm into the provided file stream */
void UA_genTal(UASTNode *tree, FILE *out);
void UA_genTal(UASTRootNode *tree, FILE *out);
#endif

9
src/uparse.c

@ -371,6 +371,7 @@ void printNode(UASTNode *node) {
case NODE_DIV: printf("DIV"); break;
case NODE_ASSIGN: printf("ASSIGN"); break;
case NODE_INTLIT: printf("[%d]", ((UASTIntNode*)node)->num); break;
case NODE_TREEROOT: printf("ROOT"); break;
case NODE_STATE_PRNT: printf("PRNT"); break;
case NODE_STATE_SCOPE: printf("SCPE"); break;
case NODE_STATE_DECLARE_VAR: printf("NVAR"); break;
@ -400,9 +401,9 @@ const char* getTypeName(UVarType type) {
}
}
UASTNode *UP_parseSource(const char *src) {
UASTRootNode *UP_parseSource(const char *src) {
UParseState state;
UASTScopeNode *root = NULL;
UASTRootNode *root = NULL;
UScope *scope;
int treeIndent = 16;
@ -412,12 +413,12 @@ UASTNode *UP_parseSource(const char *src) {
scope = newScope(&state);
/* create scope node and copy the finished scope struct */
root = (UASTScopeNode*)newBaseNode(&state, state.previous, sizeof(UASTScopeNode), NODE_STATE_SCOPE, parseScope(&state, 0), NULL);
root = (UASTRootNode*)newBaseNode(&state, state.previous, sizeof(UASTRootNode), NODE_STATE_SCOPE, parseScope(&state, 0), NULL);
root->scope = *scope;
endScope(&state);
printTree((UASTNode*)root, treeIndent);
return (UASTNode*)root;
return root;
}
void UP_freeTree(UASTNode *tree) {

8
src/uparse.h

@ -20,6 +20,7 @@ typedef enum {
statement nodes below
node->left holds expression tree, node->right holds the next statement
*/
NODE_TREEROOT,
NODE_STATE_PRNT,
NODE_STATE_DECLARE_VAR,
NODE_STATE_DECLARE_FUNC,
@ -55,6 +56,11 @@ typedef struct s_UASTNode {
struct s_UASTNode *right;
} UASTNode;
typedef struct {
COMMON_NODE_HEADER;
UScope scope;
} UASTRootNode;
typedef struct {
COMMON_NODE_HEADER;
int var; /* index of the UVar */
@ -84,7 +90,7 @@ typedef struct {
const char* getTypeName(UVarType type);
/* returns the base AST node, or NULL if a syntax error occurred */
UASTNode *UP_parseSource(const char *src);
UASTRootNode *UP_parseSource(const char *src);
void UP_freeTree(UASTNode *tree);

Loading…
Cancel
Save