Browse Source

Added '<' and '>' operators

- added TOKEN_LESS, TOKEN_GREATER, TOKEN_LESS_EQUAL, and TOKEN_GREATER_EQUAL
- while these are all supported in the parser, the compiler (uasm.c) lacks support for TOKEN_LESS_EQUAL && TOKEN_GREATER_EQUAL
remotes/origin/HEAD
Seth Stubbs 5 months ago
parent
commit
a2fce92777
  1. 1
      TODO
  2. 14
      notes
  3. 2
      src/main.c
  4. 5
      src/uasm.c
  5. 4
      src/ulex.c
  6. 4
      src/ulex.h
  7. 16
      src/uparse.c
  8. 6
      src/uparse.h
  9. 7
      test.uxnc

1
TODO

@ -1 +0,0 @@
- preprocessor directive to include binary files & define their location as a pointer

14
notes

@ -1,14 +0,0 @@
5 + 3 * 2
left = 5
ADD: left = 5, right = ?
MUL: left = 3, right = 2
ADD: left = 5, right = MUL
ADD
5 MUL
3 ASSIGN
B 2

2
src/main.c

@ -4,7 +4,7 @@
int main() {
UASTRootNode *tree = UP_parseSource(
"int i = 0;\n"
"while (i != 10) {\n"
"while (i < 10) {\n"
" prntint i;\n"
" i = i + 1;\n"
"}\n"

5
src/uasm.c

@ -349,7 +349,10 @@ UVarType compileExpression(UCompState *state, UASTNode *node) {
case NODE_MUL: doArith(state, "MUL", lType); break;
case NODE_DIV: doArith(state, "DIV", lType); break;
case NODE_EQUAL: doComp(state, "EQU", lType); return TYPE_BOOL;
case NODE_NOTEQUAL: doComp(state, "NEQ", lType); return TYPE_BOOL;
case NODE_NEQUAL: doComp(state, "NEQ", lType); return TYPE_BOOL;
case NODE_LESS: doComp(state, "LTH", lType); return TYPE_BOOL;
case NODE_GREATER: doComp(state, "GTH", lType); return TYPE_BOOL;
/* TODO: NODE_LESS_EQUAL && NODE_GREATER_EQUAL */
case NODE_INTLIT: writeIntLit(state, ((UASTIntNode*)node)->num); return TYPE_INT;
case NODE_VAR: return compileVar(state, node); break;
default:

4
src/ulex.c

@ -199,11 +199,13 @@ UToken UL_scanNext(ULexState *state) {
case '}': return makeToken(state, TOKEN_RIGHT_BRACE);
case '[': return makeToken(state, TOKEN_LEFT_BRACKET);
case ']': return makeToken(state, TOKEN_RIGHT_BRACKET);
case ';': return makeToken(state, TOKEN_COLON);
case '+': return makeToken(state, TOKEN_PLUS);
case '-': return makeToken(state, TOKEN_MINUS);
case '/': return makeToken(state, TOKEN_SLASH);
case '*': return makeToken(state, TOKEN_STAR);
case ';': return makeToken(state, TOKEN_COLON);
case '>': return charMatch(state, '=') ? makeToken(state, TOKEN_GREATER_EQUAL) : makeToken(state, TOKEN_GREATER);
case '<': return charMatch(state, '=') ? makeToken(state, TOKEN_LESS_EQUAL) : makeToken(state, TOKEN_LESS);
case '=': return charMatch(state, '=') ? makeToken(state, TOKEN_EQUAL_EQUAL) : makeToken(state, TOKEN_EQUAL);
case '!': return charMatch(state, '=') ? makeToken(state, TOKEN_BANG_EQUAL) : makeToken(state, TOKEN_BANG);
case '\'': return readCharacter(state);

4
src/ulex.h

@ -32,10 +32,14 @@ typedef enum {
TOKEN_SLASH,
TOKEN_STAR,
TOKEN_BANG,
TOKEN_LESS,
TOKEN_GREATER,
/* two character tokens */
TOKEN_EQUAL_EQUAL,
TOKEN_BANG_EQUAL,
TOKEN_LESS_EQUAL,
TOKEN_GREATER_EQUAL,
TOKEN_EOF, /* end of file */
TOKEN_UNREC, /* unrecognized symbol */

16
src/uparse.c

@ -188,8 +188,12 @@ UASTNode* binOperator(UParseState *state, UASTNode *left, Precedence currPrec) {
case TOKEN_MINUS: type = NODE_SUB; break;
case TOKEN_STAR: type = NODE_MUL; break;
case TOKEN_SLASH: type = NODE_DIV; break;
case TOKEN_LESS: type = NODE_LESS; break;
case TOKEN_GREATER: type = NODE_GREATER; break;
case TOKEN_EQUAL_EQUAL: type = NODE_EQUAL; break;
case TOKEN_BANG_EQUAL: type = NODE_NOTEQUAL; break;
case TOKEN_BANG_EQUAL: type = NODE_NEQUAL; break;
case TOKEN_LESS_EQUAL: type = NODE_LESS_EQUAL; break;
case TOKEN_GREATER_EQUAL: type = NODE_GREATER_EQUAL; break;
default:
error(state, "Unknown binary operator '%.*s'!", state->current.len, state->current.str);
return NULL;
@ -245,9 +249,13 @@ ParseRule ruleTable[] = {
{NULL, binOperator, PREC_FACTOR}, /* TOKEN_SLASH */
{NULL, binOperator, PREC_FACTOR}, /* TOKEN_STAR */
{NULL, NULL, PREC_NONE}, /* TOKEN_BANG */
{NULL, binOperator, PREC_COMPAR}, /* TOKEN_LESS */
{NULL, binOperator, PREC_COMPAR}, /* TOKEN_GREATER */
{NULL, binOperator, PREC_COMPAR}, /* TOKEN_EQUAL_EQUAL */
{NULL, binOperator, PREC_COMPAR}, /* TOKEN_BANG_EQUAL */
{NULL, binOperator, PREC_COMPAR}, /* TOKEN_LESS_EQUAL */
{NULL, binOperator, PREC_COMPAR}, /* TOKEN_GREATER_EQUAL */
{NULL, NULL, PREC_NONE}, /* TOKEN_EOF */
{NULL, NULL, PREC_NONE}, /* TOKEN_UNREC */
{NULL, NULL, PREC_NONE}, /* TOKEN_ERR */
@ -419,6 +427,12 @@ void printNode(UASTNode *node) {
case NODE_SUB: printf("SUB"); break;
case NODE_MUL: printf("MUL"); break;
case NODE_DIV: printf("DIV"); break;
case NODE_LESS: printf("LTH"); break;
case NODE_GREATER: printf("GTH"); break;
case NODE_LESS_EQUAL: printf("LEQ"); break;
case NODE_GREATER_EQUAL: printf("GEQ"); break;
case NODE_EQUAL: printf("EQU"); break;
case NODE_NEQUAL: printf("NEQ"); break;
case NODE_ASSIGN: printf("ASSIGN"); break;
case NODE_INTLIT: printf("[%d]", ((UASTIntNode*)node)->num); break;
case NODE_TREEROOT: printf("ROOT"); break;

6
src/uparse.h

@ -14,8 +14,12 @@ typedef enum {
NODE_SUB,
NODE_MUL,
NODE_DIV,
NODE_LESS,
NODE_GREATER,
NODE_EQUAL,
NODE_NOTEQUAL,
NODE_NEQUAL,
NODE_LESS_EQUAL,
NODE_GREATER_EQUAL,
/* literals */
NODE_INTLIT,
NODE_VAR,

7
test.uxnc

@ -1,7 +0,0 @@
int sub(int a, int b) {
return a - b;
}
void main() {
sub(5, 4);
}
Loading…
Cancel
Save