Value* Value_parse(const char** expr, char sep, char end, parser_cb* cb) { Value* val; BINTYPE op = BIN_UNK; BinOp* tree = NULL; BinOp* prev; while(1) { /* Get next value */ val = Value_next(expr, end, cb); /* Error parsing next value? */ if(val->type == VAL_ERR) { if(tree) { BinOp_free(tree); } return val; } /* End of input? */ if(val->type == VAL_END) { if(tree) { BinOp_free(tree); } return val; } /* Special case: negative value */ if(val->type == VAL_NEG) { Value_free(val); BinOp* cur = BinOp_new(BIN_MUL, ValInt(-1), NULL); if(tree) { prev->b = ValExpr(cur); } else { tree = cur; } prev = cur; continue; } /* Get next operator if it exists */ op = BinOp_nextType(expr, sep, end); /* Invalid operator? Return syntax error */ if(op == BIN_UNK) { /* Exit gracefully and return error */ if(tree) { BinOp_free(tree); } Value_free(val); return ValErr(badChar(**expr)); } /* End of the statement? */ else if(op == BIN_END) { /* Only skip end character if there's only one value to parse */ if(!sep && **expr && **expr == end) { (*expr)++; } /* If there was only one value, return it */ if(!tree) { return val; } /* Otherwise, place the final value into the tree and break out of the parse loop */ prev->b = val; break; } /* Tree not yet begun? Initialize it! */ if(tree == NULL) { tree = BinOp_new(op, val, NULL); prev = tree; } else { /* Tree already started, so add to it */ treeAddValue(&tree, &prev, op, val); } } return ValExpr(tree); }
Expression* Expression_parse(const char** expr) { Expression* ret = NULL; Variable* var; Value* val; const char* equals = strchr(*expr, '='); if(equals == NULL) { /* No assignment, just a plain expression. */ return parseExpr(expr); } /* There is an assignment */ /* First, parse the right side of the assignment */ equals++; val = Value_parse(&equals, 0, 0); if(val->type == VAL_ERR) { /* A parse error occurred */ var = VarErr(Error_copy(val->err)); Value_free(val); return Expression_new(var); } if(val->type == VAL_END) { /* Empty input */ Value_free(val); var = VarErr(earlyEnd()); return Expression_new(var); } /* Now parse the left side */ char* name = nextToken(expr); if(name == NULL) { Value_free(val); var = VarErr(syntaxError("No variable to assign to.")); return Expression_new(var); } trimSpaces(expr); if(**expr == '(') { /* Defining a function */ (*expr)++; /* Array of argument names */ unsigned size = 2; char** args = fmalloc(size * sizeof(*args)); unsigned len = 0; /* Add each argument name to the array */ char* arg = nextToken(expr); if(arg == NULL && **expr != ')') { /* Invalid character */ Value_free(val); free(args); free(name); var = VarErr(badChar(**expr)); return Expression_new(var); } trimSpaces(expr); if(arg == NULL) { /* Empty parameter list means function with no args */ free(args); args = NULL; len = 0; } else { /* Loop through each argument in the list */ while(**expr == ',' || **expr == ')') { args[len++] = arg; if(**expr == ')') break; (*expr)++; /* Expand argument array if it's too small */ if(len >= size) { size *= 2; args = frealloc(args, size * sizeof(*args)); } arg = nextToken(expr); if(arg == NULL) { /* Invalid character */ Value_free(val); free(name); /* Free argument names and return */ unsigned i; for(i = 0; i < len; i++) { free(args[i]); } free(args); var = VarErr(badChar(**expr)); return Expression_new(var); } trimSpaces(expr); } } if(**expr != ')') { /* Invalid character inside argument name list */ Value_free(val); free(name); /* Free argument names and return */ unsigned i; for(i = 0; i < len; i++) { free(args[i]); } free(args); var = VarErr(badChar(**expr)); return Expression_new(var); } /* Skip closing parenthesis */ (*expr)++; trimSpaces(expr); if(**expr != '=') { Value_free(val); free(name); unsigned i; for(i = 0; i < len; i++) { free(args[i]); } free(args); var = VarErr(badChar(**expr)); return Expression_new(var); } /* Construct function and return it */ Function* func = Function_new(len, args, val); var = VarFunc(name, func); free(name); ret = Expression_new(var); } else { /* Defining a variable */ if(**expr != '=') { /* In-place manipulation */ BINTYPE bin = BinOp_nextType(expr, 0, 0); /* Still not an equals sign means invalid character */ if(**expr != '=') { Value_free(val); free(name); var = VarErr(badChar(**expr)); return Expression_new(var); } val = ValExpr(BinOp_new(bin, ValVar(name), val)); } var = VarValue(name, val); free(name); ret = Expression_new(var); } return ret; }