stderr
This commit is contained in:
parent
26bb0a8df1
commit
49329273d3
3 changed files with 23 additions and 17 deletions
|
|
@ -62,7 +62,7 @@ Environment insert(char* name, int n, Memory memory, Environment env) {
|
||||||
Environment new_env = (Environment) {.next = cell};
|
Environment new_env = (Environment) {.next = cell};
|
||||||
return new_env;
|
return new_env;
|
||||||
}
|
}
|
||||||
printf("Error: out of memory.");
|
fprintf(stderr, "Error: out of memory.");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -93,7 +93,7 @@ int eval_expr(Expr expr, Memory memory, Environment env) {
|
||||||
case VARIABLE: {
|
case VARIABLE: {
|
||||||
int* result = lookup(expr.data.variable, memory, env);
|
int* result = lookup(expr.data.variable, memory, env);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
printf("Error: variable not found '%s'\n", expr.data.variable);
|
fprintf(stderr, "Error: variable not found '%s'\n", expr.data.variable);
|
||||||
exit(1);
|
exit(1);
|
||||||
} else {
|
} else {
|
||||||
return *result;
|
return *result;
|
||||||
|
|
@ -106,7 +106,7 @@ int eval_expr(Expr expr, Memory memory, Environment env) {
|
||||||
printf("%d\n", arg);
|
printf("%d\n", arg);
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
printf("Error: print expects a single argument.\n");
|
fprintf(stderr, "Error: print expects a single argument.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -116,7 +116,7 @@ int eval_expr(Expr expr, Memory memory, Environment env) {
|
||||||
int arg2 = eval_expr(((Expr*)expr.data.function.args.elements)[1], memory, env);
|
int arg2 = eval_expr(((Expr*)expr.data.function.args.elements)[1], memory, env);
|
||||||
return arg1 + arg2;
|
return arg1 + arg2;
|
||||||
} else {
|
} else {
|
||||||
printf("Error: negate expects a single argument.\n");
|
fprintf(stderr, "Error: negate expects a single argument.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -125,7 +125,7 @@ int eval_expr(Expr expr, Memory memory, Environment env) {
|
||||||
int arg = eval_expr(((Expr*)expr.data.function.args.elements)[0], memory, env);
|
int arg = eval_expr(((Expr*)expr.data.function.args.elements)[0], memory, env);
|
||||||
return 0 - arg;
|
return 0 - arg;
|
||||||
} else {
|
} else {
|
||||||
printf("Error: negate expects a single argument.\n");
|
fprintf(stderr, "Error: negate expects a single argument.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ TokenArray lex(char* txt, unsigned length) {
|
||||||
tokens[tokens_index] = (Token){ .tag = INTEGER, .data.integer = integer, };
|
tokens[tokens_index] = (Token){ .tag = INTEGER, .data.integer = integer, };
|
||||||
++tokens_index;
|
++tokens_index;
|
||||||
} else {
|
} else {
|
||||||
printf("unexpected character '%c'", txt[txt_index]);
|
fprintf(stderr, "unexpected character '%c'", txt[txt_index]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ int maybe_parse_token(TokenArray tokens, unsigned* tokens_index, TokenTag tag) {
|
||||||
|
|
||||||
void parse_token(TokenArray tokens, unsigned* tokens_index, TokenTag tag) {
|
void parse_token(TokenArray tokens, unsigned* tokens_index, TokenTag tag) {
|
||||||
if (!(maybe_parse_token(tokens, tokens_index, tag))) {
|
if (!(maybe_parse_token(tokens, tokens_index, tag))) {
|
||||||
printf("Parse error: unexpected token. Expected %d\n", tag);
|
fprintf(stderr, "Parse error: unexpected token. Expected %d\n", tag);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -31,12 +31,12 @@ char* parse_identifier(TokenArray tokens, unsigned* tokens_index) {
|
||||||
Token token = tokens.tokens[*tokens_index];
|
Token token = tokens.tokens[*tokens_index];
|
||||||
++(*tokens_index);
|
++(*tokens_index);
|
||||||
if (token.tag != IDENTIFIER) {
|
if (token.tag != IDENTIFIER) {
|
||||||
printf("Parse error: got wrong token: %d, expected IDENTIFIER\n", token.tag);
|
fprintf(stderr, "Parse error: got wrong token: %d, expected IDENTIFIER\n", token.tag);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
return token.data.identifier;
|
return token.data.identifier;
|
||||||
} else {
|
} else {
|
||||||
printf("Parse error: unexpected end of text.\n");
|
fprintf(stderr, "Parse error: unexpected end of text.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -54,6 +54,7 @@ Expr parse_function(TokenArray tokens, unsigned* tokens_index, char* name) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parse_token(tokens, tokens_index, CLOSEPAREN);
|
parse_token(tokens, tokens_index, CLOSEPAREN);
|
||||||
|
|
||||||
return (Expr) {
|
return (Expr) {
|
||||||
.tag = FUNCTION,
|
.tag = FUNCTION,
|
||||||
.data.function = {
|
.data.function = {
|
||||||
|
|
@ -88,11 +89,11 @@ Expr parse_expr(TokenArray tokens, unsigned* tokens_index) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
printf("Parse error: got wrong token: %d, expected IDENTIFIER or INTEGER\n", token.tag);
|
fprintf(stderr, "Parse error: got wrong token: %d, expected IDENTIFIER or INTEGER\n", token.tag);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
printf("Parse error: unexpected end of text.\n");
|
fprintf(stderr, "Parse error: unexpected end of text.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -102,21 +103,24 @@ StmtArray parse_block(TokenArray tokens, unsigned* tokens_index);
|
||||||
Stmt parse_stmt(TokenArray tokens, unsigned* tokens_index) {
|
Stmt parse_stmt(TokenArray tokens, unsigned* tokens_index) {
|
||||||
char* identifier = parse_identifier(tokens, tokens_index);
|
char* identifier = parse_identifier(tokens, tokens_index);
|
||||||
|
|
||||||
|
// While
|
||||||
if (is_while(identifier)) {
|
if (is_while(identifier)) {
|
||||||
Expr expr = parse_expr(tokens, tokens_index);
|
Expr condition = parse_expr(tokens, tokens_index);
|
||||||
|
|
||||||
StmtArray block = parse_block(tokens, tokens_index);
|
StmtArray block = parse_block(tokens, tokens_index);
|
||||||
|
|
||||||
return (Stmt) {
|
return (Stmt) {
|
||||||
.tag = WHILE,
|
.tag = WHILE,
|
||||||
.data.While = {
|
.data.While = {
|
||||||
.condition = expr,
|
.condition = condition,
|
||||||
.block = {
|
.block = {
|
||||||
.elements = block.stmts,
|
.elements = block.stmts,
|
||||||
.length = block.length,
|
.length = block.length,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
} else if (maybe_parse_token(tokens, tokens_index, EQUALS)) {
|
}
|
||||||
|
// Set
|
||||||
|
else if (maybe_parse_token(tokens, tokens_index, EQUALS)) {
|
||||||
Expr expr = parse_expr(tokens, tokens_index);
|
Expr expr = parse_expr(tokens, tokens_index);
|
||||||
|
|
||||||
return (Stmt) {
|
return (Stmt) {
|
||||||
|
|
@ -126,7 +130,9 @@ Stmt parse_stmt(TokenArray tokens, unsigned* tokens_index) {
|
||||||
.expr = expr,
|
.expr = expr,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
} else {
|
}
|
||||||
|
// Expression
|
||||||
|
else {
|
||||||
Expr expr = parse_function(tokens, tokens_index, identifier);
|
Expr expr = parse_function(tokens, tokens_index, identifier);
|
||||||
|
|
||||||
return (Stmt) {
|
return (Stmt) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue