diff options
author | Tavian Barnes <tavianator@gmail.com> | 2010-03-22 22:07:29 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2010-03-22 22:07:29 -0400 |
commit | 7fc84f364bcb2e37363d3e22300180ba92288811 (patch) | |
tree | 66d12cb09c22d4df311feffd4d418bfae6ba5568 /dimension/parse.c | |
parent | 80b15832f59b5a5b4e7476c6eb4099b6edccbb1b (diff) | |
download | dimension-7fc84f364bcb2e37363d3e22300180ba92288811.tar.xz |
Support the ternary operator.
Diffstat (limited to 'dimension/parse.c')
-rw-r--r-- | dimension/parse.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/dimension/parse.c b/dimension/parse.c index 0f94f6c..52479ad 100644 --- a/dimension/parse.c +++ b/dimension/parse.c @@ -1413,6 +1413,35 @@ dmnsn_eval_binary(dmnsn_astnode astnode, dmnsn_symbol_table *symtable) return ret; } +static dmnsn_astnode +dmnsn_eval_ternary(dmnsn_astnode astnode, dmnsn_symbol_table *symtable) +{ + dmnsn_astnode test, iftrue, iffalse, ret; + dmnsn_array_get(astnode.children, 0, &test); + dmnsn_array_get(astnode.children, 1, &iftrue); + dmnsn_array_get(astnode.children, 2, &iffalse); + test = dmnsn_eval_scalar(test, symtable); + + switch(astnode.type) { + case DMNSN_AST_TERNARY: + dmnsn_assert(test.type == DMNSN_AST_INTEGER, + "Conditional expression evaluated to non-integer."); + ret = dmnsn_eval(*(long *)test.ptr ? iftrue : iffalse, symtable); + break; + + default: + dmnsn_diagnostic(astnode.filename, astnode.line, astnode.col, + "invalid ternary operator '%s'", + dmnsn_astnode_string(astnode.type)); + ret = dmnsn_copy_astnode(astnode); + ret.type = DMNSN_AST_NONE; + break; + } + + dmnsn_delete_astnode(test); + return ret; +} + dmnsn_astnode dmnsn_eval(dmnsn_astnode astnode, dmnsn_symbol_table *symtable) { @@ -1513,6 +1542,9 @@ dmnsn_eval(dmnsn_astnode astnode, dmnsn_symbol_table *symtable) case DMNSN_AST_VDOT: return dmnsn_eval_binary(astnode, symtable); + case DMNSN_AST_TERNARY: + return dmnsn_eval_ternary(astnode, symtable); + default: dmnsn_diagnostic(astnode.filename, astnode.line, astnode.col, "expected arithmetic expression; found %s", |