Expression *newFunctionExpression(Scope *scope, char *identifier, Expression *arguments) { ScopeElement *elem = findScopeElement(scope, identifier); Expression *expr = NULL; if(elem) { if(elem->elementType == SCOPE_FUNC) { FunctionExpression *functionExpression = malloc(sizeof(FunctionExpression)); expr = newExpression(); // Create the function expression ScopeFunction *func = elem->function; functionExpression->identifier = identifier; functionExpression->returnType = func->returnType; functionExpression->arguments = arguments; // Wrap it expr->functionExpression = functionExpression; expr->type = FUNCTION; debug(E_DEBUG, "Creating function expression for ID %s\n", identifier); } else { error(VAR_AS_FUNCTION, identifier); } } else { error(CALL_UNDEF_FUNCTION, identifier); } typeCheckExpression(expr); return expr; }
Expression *newVariableExpression(Scope *scope, char *identifier, Expression *arrayIndex) { ScopeElement *elem = findScopeElement(scope, identifier); VariableExpression *variableExpression = malloc(sizeof(VariableExpression)); Expression *expr = newExpression(); if(elem) { if(elem->elementType == SCOPE_VAR) { // Create the variable expression ScopeVariable *var = elem->variable; Type varType = var->type; variableExpression->type = varType; variableExpression->identifier = identifier; variableExpression->arrayIndex = arrayIndex; // Wrap it expr->variableExpression = variableExpression; expr->type = VARIABLE; debug(E_DEBUG, "Creating variable expression for ID %s\n", identifier); } else { error(VAR_AS_FUNCTION, identifier); } } else { error(UNDECLARED_INDENTIFIER, identifier); foundError = true; } typeCheckExpression(expr); return expr; }
Expression *newConstExpression(Type type, Value *value) { ConstExpression *constExpr = malloc(sizeof(ConstExpression)); constExpr->type = type; constExpr->value = value; Expression *expr = newExpression(); expr->constantExpression = constExpr; expr->type = CONST; expr->inferredType = type; return expr; }
Expression *newUnaryExpression(UnaryOperation op, Expression *operand) { UnaryExpression *unaryExpr = malloc(sizeof(UnaryExpression)); unaryExpr->operation = op; unaryExpr->operand = operand; Expression *expr = newExpression(); expr->unaryExpression = unaryExpr; expr->type = UNARY; debug(E_DEBUG, "Creating unary expression with operand of type %s\n", expressionTypeName(operand)); typeCheckExpression(expr); return expr; }
Expression *newBinaryExpression(BinaryOperation op, Expression *left, Expression *right) { BinaryExpression *binaryExpr = malloc(sizeof(BinaryExpression)); binaryExpr->operation = op; binaryExpr->leftOperand = left; binaryExpr->rightOperand = right; Expression *expr = newExpression(); expr->binaryExpression = binaryExpr; expr->type = BINARY; debug(E_DEBUG, "Creating binary expression with left operand types %s and %s\n", expressionTypeName(left), expressionTypeName(right)); typeCheckExpression(expr); return expr; }
*/ #include <stdlib.h> #include <string.h> #include "../../testing/cspec.h" #include "../../testing/cspec_output_unit.h" #include "../constructors.h" #include "../constants.h" #include "../strings.h" #include "../../primitives/prim_int.h" DESCRIBE(newExpression, "expression* newExpression ()") IT("Creates a nil expression") expression* expr = newExpression(); SHOULD_EQUAL(expr->type, TYPE_NIL) freeExpr(expr); END_IT END_DESCRIBE DESCRIBE(newExpressionOfType, "expression* newExpressionOfType (datatype type)") IT("Creates an expression with the given type and initializes it to the default value for that type") expression* expr1 = newExpressionOfType(TYPE_INT); SHOULD_EQUAL(expr1->ev.intval, 0) freeExpr(expr1); expression* expr2 = newExpressionOfType(TYPE_ARR); SHOULD_EQUAL(expr2->ev.arrval->size, 0) freeExpr(expr2); END_IT END_DESCRIBE