Browse Source

Minor refactoring, added NODE_STATE_DECLARE_VAR

- renamed uasm.c:compileInt to compileDeclaration
remotes/origin/HEAD
Seth Stubbs 5 months ago
parent
commit
01cf747869
  1. 8
      src/uasm.c
  2. 4
      src/uparse.c
  3. 3
      src/uparse.h

8
src/uasm.c

@ -299,7 +299,7 @@ void compilePrintInt(UCompState *state, UASTNode *node) {
state->pushed -= SIZE_INT;
}
void compileInt(UCompState *state, UASTNode *node) {
void compileDeclaration(UCompState *state, UASTNode *node) {
UVarType type;
UASTVarNode *var = (UASTVarNode*)node;
UVar *rawVar = getVarByID(state, var->scope, var->var);
@ -307,8 +307,8 @@ void compileInt(UCompState *state, UASTNode *node) {
/* if there's no assignment, the default value will be scary undefined memory :O */
if (node->left) {
type = compileExpression(state, node->left);
if (type != TYPE_INT)
cError(state, "Cannot assign type '%s' to %.*s of type 'int'", getTypeName(type), rawVar->len, rawVar->name);
if (compareVarTypes(state, type, rawVar->type))
cError(state, "Cannot assign type '%s' to %.*s of type '%s'", getTypeName(type), rawVar->len, rawVar->name, getTypeName(rawVar->type));
setIntVar(state, var->scope, var->var);
}
}
@ -328,7 +328,7 @@ void compileAST(UCompState *state, UASTNode *node) {
while (node) {
switch(node->type) {
case NODE_STATE_PRNT: compilePrintInt(state, node); break;
case NODE_STATE_INT: compileInt(state, node); break;
case NODE_STATE_DECLARE_VAR: compileDeclaration(state, node); break;
case NODE_STATE_EXPR: compileExpression(state, node->left); break;
case NODE_STATE_SCOPE: compileScope(state, node); break;
default:

4
src/uparse.c

@ -302,7 +302,7 @@ UASTNode* intStatement(UParseState *state) {
var = newVar(state, TYPE_INT, state->previous.str, state->previous.len);
/* if it's assigned a value, evaluate the expression & set the left node, if not set it to NULL */
node = (UASTVarNode*)newBaseNode(state, sizeof(UASTVarNode), NODE_STATE_INT, (match(state, TOKEN_EQUAL)) ? expression(state) : NULL, NULL);
node = (UASTVarNode*)newBaseNode(state, sizeof(UASTVarNode), NODE_STATE_DECLARE_VAR, (match(state, TOKEN_EQUAL)) ? expression(state) : NULL, NULL);
node->var = var;
node->scope = state->sCount-1;
return (UASTNode*)node;
@ -362,7 +362,7 @@ void printNode(UASTNode *node) {
case NODE_INTLIT: printf("[%d]", ((UASTIntNode*)node)->num); break;
case NODE_STATE_PRNT: printf("PRNT"); break;
case NODE_STATE_SCOPE: printf("SCPE"); break;
case NODE_STATE_INT: printf("INT"); break;
case NODE_STATE_DECLARE_VAR: printf("NVAR"); break;
case NODE_VAR: printf("VAR[%d]", ((UASTVarNode*)node)->var); break;
case NODE_STATE_EXPR: printf("EXPR"); break;
default: break;

3
src/uparse.h

@ -21,7 +21,8 @@ typedef enum {
node->left holds expression tree, node->right holds the next statement
*/
NODE_STATE_PRNT,
NODE_STATE_INT,
NODE_STATE_DECLARE_VAR,
NODE_STATE_DECLARE_FUNC,
NODE_STATE_EXPR,
/* scopes are different, node->left holds the statement tree for the scope, node->right holds the next statement */
NODE_STATE_SCOPE,

Loading…
Cancel
Save