Node* insertStore(char* id, Node* arrayIndex, Node* expression){ Node* thisNode = (Node*) calloc(sizeof(Node),1); if(thisNode == NULL) { if(DEBUG)printf("Error in malloc [insertStore]\n"); assert(thisNode == NULL); } thisNode->value = NULL; thisNode->n1 = createTerminalNode(NODE_ID,id); thisNode->n2 = arrayIndex; if(arrayIndex != NULL) { thisNode->n_type = NODE_STOREARRAY; if(arrayIndex == NULL) thisNode->n2 = createNull(); } else thisNode->n_type = NODE_STORE; thisNode->n3 = expression; if(expression == NULL) thisNode->n3 = createNull(); thisNode->next = NULL; return thisNode; }
Node* insertExpression(char* op,Node* exp){ Node * newExpression = (Node*)calloc(sizeof(Node),1); if(newExpression == NULL) { if(DEBUG) printf("newCall: Error Malloc\n"); assert(newExpression != NULL); } newExpression->n_type = getOperatorType(op); free(op); if(newExpression->n_type == -1 ) exit(-2); if(newExpression->n_type == NODE_PLUS) newExpression->n_type = NODE_UNARYPLUS; if(newExpression->n_type == NODE_MINUS) newExpression->n_type = NODE_UNARYMINUS; newExpression->n1 = exp; if(exp == NULL) newExpression->n1 = createNull(); newExpression->next = NULL; return newExpression; }
Node* insertLoadArray(Node* expression, Node* indexExpression) { Node* newLoadArray = (Node*) calloc(sizeof(Node),1); if(newLoadArray == NULL){ if(DEBUG)printf("Error in malloc insertDotLength"); assert(newLoadArray != NULL); } newLoadArray->n_type = NODE_LOADARRAY; newLoadArray->n1 = expression; newLoadArray->n2 = indexExpression; if(expression == NULL) newLoadArray->n1 = createNull(); if(indexExpression == NULL) newLoadArray->n2 = createNull();; newLoadArray->next = NULL; return newLoadArray; }
CXString createDup(const char *String) { if (!String) return createNull(); if (String[0] == '\0') return createEmpty(); CXString Str; Str.data = strdup(String); Str.private_flags = CXS_Malloc; return Str; }
/* Method to insert a While Node */ Node* insertWhile(Node* expression, Node* statements) { Node* newWhile = (Node*) calloc(sizeof(Node),1); if(newWhile == NULL){ if(DEBUG)printf("Error in malloc [insertCompound]\n"); assert(newWhile != NULL); } newWhile->n_type = NODE_WHILE; newWhile->n1 = expression; newWhile->n2 = statements; if(expression == NULL) newWhile->n1 = createNull(); if(statements == NULL) newWhile->n2 = createNull(); newWhile->next = NULL; return newWhile; }
Node* insertIf(Node* expression, Node* statement1, Node* statement2 ) { Node* insertIf = (Node*) calloc(sizeof(Node),1); if(insertIf == NULL) { if(DEBUG)printf("Error in MALLOC insertIf\n"); assert(insertIf != NULL); } insertIf->n_type = NODE_IFELSE; insertIf->n1 = expression; insertIf->n2 = statement1; insertIf->n3 = statement2; insertIf->next = NULL; if(expression == NULL) insertIf->n1 = createNull(); if(statement1 == NULL) insertIf->n2 = createNull(); if(statement2 == NULL) insertIf->n3 = createNull(); return insertIf; }
Node* insertDotLength(Node* expression){ Node* newDotLength = (Node*) calloc(sizeof(Node),1); if(newDotLength == NULL){ if(DEBUG)printf("Error in malloc insertDotLength"); assert(newDotLength != NULL); } newDotLength->n_type = NODE_LENGTH; newDotLength->n1 = expression; if(expression == NULL) newDotLength->n1 = createNull(); newDotLength->next = NULL; return newDotLength; }
Node* insertDoubleExpression(Node* exp1,char* op,Node* exp2){ Node * newExpression = (Node*)calloc(sizeof(Node),1); if(newExpression == NULL) { if(DEBUG) printf("newCall: Error Malloc\n"); assert(newExpression != NULL); } newExpression-> n_type = getOperatorType(op); free(op); if(newExpression->n_type == -1 ) exit(-2); newExpression->n1 = exp1; newExpression->n2 = exp2; if(exp1 == NULL) newExpression->n1 = createNull(); if(exp2 == NULL) newExpression->n2 = createNull(); newExpression->next = NULL; return newExpression; }
Node* insertParseInt(char* id, Node* indexExpression){ Node* newParseInt = (Node*) calloc(sizeof(Node),1); if(newParseInt == NULL){ if(DEBUG)printf("Error in malloc insertDotLength"); assert(newParseInt != NULL); } newParseInt->n_type = NODE_PARSEARGS; newParseInt->n1 = createTerminalNode(NODE_ID,id); newParseInt->n2 = indexExpression; if(indexExpression == NULL) newParseInt->n2 = createNull(); newParseInt->next = NULL; return newParseInt; }
Node* insertPrint(Node* expression) { Node* newPrint = (Node*) calloc(sizeof(Node),1); if(newPrint == NULL) { if(DEBUG)printf("Error in malloc insertCompound\n"); assert(newPrint != NULL); } newPrint->n_type = NODE_PRINT; newPrint->n1 = expression; if(expression == NULL) newPrint->n1 = createNull(); newPrint->next = NULL; return newPrint; }
Node * insertNewArray(int type, Node* expression) { Node * newArray = (Node*)calloc(sizeof(Node),1); if(newArray == NULL) { if(DEBUG) printf("newArray: Error Malloc\n"); assert(newArray != NULL); } newArray-> n_type = type; newArray->n1 = expression; if(expression == NULL) newArray->n1 = createNull(); if(type == NODE_NEWBOOL) newArray->type = TYPE_BOOL_ARRAY; if(type == NODE_NEWINT) newArray->type = TYPE_INT_ARRAY; newArray->next = NULL; return newArray; }
void aJsonClass::addNullToObject(aJsonObject* object, const char* name) { addItemToObject(object, name, createNull()); }
// read value at file pos bool CJson:: readValue(CStrParse &parse, ValueP &value) { if (parse.eof()) return false; char c = parse.getCharAt(); if (c == '\"') { std::string str1; if (! readString(parse, str1)) return false; value = ValueP(createString(str1)); } else if (c == '-' || isdigit(c)) { std::string str1; if (! readNumber(parse, str1)) return false; bool ok; double n = CJson::stod(str1, ok); value = ValueP(createNumber(n)); } else if (c == '{') { Object *obj; if (! readObject(parse, obj)) return false; value = ValueP(obj); } else if (c == '[') { Array *array; if (! readArray(parse, array)) return false; value = ValueP(array); } else if (parse.isString("true")) { parse.skipChars("true"); value = ValueP(createTrue()); } else if (parse.isString("false")) { parse.skipChars("false"); value = ValueP(createFalse()); } else if (parse.isString("null")) { parse.skipChars("null"); value = ValueP(createNull()); } else return false; return true; }
TValue vm_interpret(AttoVM* vm, AttoBlock* block, int start, int argc) { DEBUGF("Interpret block: %d ops\n", block->code->size); int error = 0; TValue* max = (TValue*)(block->code->elements + start + block->code->size); TValue *pc_val = (TValue*)(block->code->elements + start); Instruction i = TV2INST(*pc_val); Stack *stack = &block->stack; const char* opcode_names[] = { OPCODE_NAMES }; const char* op_name = NULL; int x; for(x = 0; pc_val < max && !error; ++x) { op_name = i >= NUM_OPS ? "unknown" : opcode_names[i]; if(i >= NUM_OPS) { ERROR("bad opcode: %d", i); } DEBUGF("[%d]\t%s (%d)\n", x, op_name, i); switch(i) { case OP_NOP: DISPATCH; case OP_POP: EXPECT_ON_STACK(1); TValue v = pop(stack); valueDestroy(&v); DISPATCH; case OP_DUP: { EXPECT_ON_STACK(1); TValue v = pop(stack); push(stack, v); push(stack, v); DISPATCH; } case OP_SWAP: { EXPECT_ON_STACK(2); TValue f = pop(stack); TValue s = pop(stack); push(stack, f); push(stack, s); DISPATCH; } case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_MOD: case OP_POW: { EXPECT_ON_STACK(2); TValue b = pop(stack); TValue a = pop(stack); TValue res = MathOp(i, a, b); push(stack, res); DISPATCH; } case OP_OR: case OP_AND: case OP_XOR: { EXPECT_ON_STACK(2); TValue b = pop(stack); TValue a = pop(stack); TValue res = BitwiseOp(i, a, b); push(stack, res); DISPATCH; } case OP_NOT: { EXPECT_ON_STACK(1); TValue a = pop(stack); push(stack, createNumber(~(long)TV2NUM(a))); DISPATCH; } case OP_EQ: case OP_LT: case OP_GT: case OP_LTE: case OP_GTE: case OP_CMP: { EXPECT_ON_STACK(2); TValue b = pop(stack); TValue a = pop(stack); TValue res = ComparisonOp(i, a, b); push(stack, res); DISPATCH; } case OP_IF: { EXPECT_ON_STACK(1); TValue t = pop(stack); if(boolValue(t)) { NEXTINST; } DISPATCH; } case OP_JMP: { EXPECT_ON_STACK(1); long jmp = (long)TV2NUM(pop(stack)); if(jmp + pc_val >= max || jmp + (long)pc_val < 0) { ERROR("Invalid jump: %ld", jmp); } pc_val += jmp; DISPATCH; } case OP_PUSHCONST: { int index = TV2INST(*++pc_val); if(index >= (int)block->k->size) { ERROR("Constant index out of bounds: %d", index); } TValue k = getIndex(block->k, index); push(stack, k); DISPATCH; } case OP_PUSHVAR: { int index = TV2INST(*++pc_val); if(index < 0 || index >= block->sizev) { ERROR("Variable index out of bounds: %d", index); } DEBUGF("PUSHVAR, index %d, value >> %s\n", index, TValue_to_string(block->vars[index])); TValue var = block->vars[index]; var.value.var.index = index; block->vars[index] = var; push(stack, var); DISPATCH; } case OP_SETVAR: { EXPECT_ON_STACK(2); TValue var = pop(stack); if(var.type != TYPE_VAR) { ERROR("Expected a var, but got %s", TValue_type_to_string(var)); } int index = var.value.var.index; TValue *val = malloc(sizeof(TValue)); *val = pop(stack); block->vars[index] = createVar(val); DEBUGF("SETVAR, index %d, value >> %s\n", index, TValue_to_string(block->vars[index])); DISPATCH; } case OP_VALUEVAR: { EXPECT_ON_STACK(1); TValue var = pop(stack); if(var.type != TYPE_VAR) { ERROR("Expected a var, but got %s", TValue_type_to_string(var)); } Value val = *var.value.var.value; AttoType type = var.value.var.type; TValue t; t.value = val; t.type = type; push(stack, t); DISPATCH; } case OP_BOOLVALUE: { EXPECT_ON_STACK(1); TValue tos = pop(stack); int bool = boolValue(tos); valueDestroy(&tos); push(stack, createBool(bool)); DISPATCH; } case OP_CONCAT: { EXPECT_ON_STACK(2); TValue top = pop(stack); TValue sec = pop(stack); if(!(top.type == TYPE_STRING && sec.type == TYPE_STRING)) { ERROR("Expected two string values, but got %s and %s", TValue_type_to_string(sec), TValue_type_to_string(top)); } char *top_s = TV2STR(top); char *sec_s = TV2STR(sec); char *str = malloc(strlen(top_s) + strlen(sec_s)); strcpy(str, sec_s); strcat(str, top_s); valueDestroy(&top); valueDestroy(&sec); push(stack, createString(str, strlen(str), 0)); free(str); DISPATCH; } case OP_PRINT: { EXPECT_ON_STACK(1); TValue v = pop(stack); char *str = TValue_to_string(v); printf("%s", str); if(v.type == TYPE_NUMBER) free(str); valueDestroy(&v); DISPATCH; } case OP_READLINE: { char *buf = malloc(BUFSIZ); memset(buf, '\0', BUFSIZ); if(fgets(buf, BUFSIZ, stdin) ) { char *nl = strchr(buf, '\n'); if(nl) { *nl = '\0'; } } unsigned len = strlen(buf) + 1; push(stack, createString(buf, len, 0)); free(buf); DISPATCH; } case OP_DUMPSTACK: print_stack(*stack); DISPATCH; case OP_CLEARSTACK: { Stack s = StackNew(); StackDestroy(&block->stack); *stack = s; DISPATCH; } case OP_CALL: { EXPECT_ON_STACK(2); TValue fcn = pop(stack); TValue num = pop(stack); // FIXME: this explodes when types aren't right :( if(fcn.type != TYPE_FUNCTION || num.type != TYPE_NUMBER) { ERROR("Expected function and numeric values, but got %s and %s", TValue_type_to_string(fcn), TValue_type_to_string(num)); } int nargs = (int)TV2NUM(num); EXPECT_ON_STACK(nargs); int j; for(j = 0; j < nargs; ++j) { TValue v = pop(stack); push(&fcn.value.function.b->stack, v); } TValue ret = vm_interpret(vm, fcn.value.function.b, 0, nargs); if(ret.type != TYPE_NULL) { push(stack, ret); } valueDestroy(&fcn); valueDestroy(&num); valueDestroy(&fcn); DISPATCH; } case OP_RETURN: { EXPECT_ON_STACK(1); TValue ret = pop(stack); DEBUGF("Finished block, returning with %s\n", TValue_to_string(ret)); return ret; DISPATCH; } default: ERROR("Unrecognized opcode: %d", i); } } DEBUGLN("Finished block"); return createNull(); }