int compareWithNoCase2(const char_type* str1, const char_type* str2) { char_type *str1_2 = new char_type[type_length(str1)]; char_type *str2_2 = new char_type[type_length(str2)]; type_copy(str1_2, str1); type_copy(str2_2, str2); toupper(*str1_2); toupper(*str2_2); int result = type_compare(str1_2, str2_2); delete[] str1_2; delete[] str2_2; return result; }
struct type *type_copy(struct type *t) { if(!t) return 0; struct type *new_type = malloc(sizeof(struct type)); new_type->kind = t->kind; new_type->params = t->params; new_type->subtype = type_copy(t->subtype); return new_type; }
type *type_setdependent(type *t) { type_debug(t); if (t->Tcount > 0 && /* if other people pointing at t */ !(t->Tflags & TFdependent)) { t = type_copy(t); } t->Tflags |= TFdependent; return t; }
int CCommonFnc::File_GetAvailableFileName(string_type baseFile, string_type* pFreeFileName) { int status = STAT_OK; char_type fileName[MAX_PATH]; string_type fileNameStr; char_type drive[_MAX_DRIVE]; char_type dir[_MAX_DIR]; char_type fname[_MAX_FNAME]; char_type ext[_MAX_EXT]; const char_type* help; DWORD index = 1; // FIND FIRST FREE INDEX type_path_split(baseFile.c_str(), drive, _MAX_DRIVE, dir, _MAX_DIR, fname, _MAX_FNAME, ext, _MAX_EXT); index = 1; string_type zeroes = _CONV("0000"); //sprintf_s(fileName, MAX_PATH, "%s%s%s%s%d%s", drive, dir, fname, (LPCTSTR) zeroes.c_str(), index, ext); fileNameStr = string_format(_CONV("%s%s%s%s%d%s"), drive, dir, fname, (LPCTSTR)zeroes.c_str(), index, ext); help = fileNameStr.c_str(); type_copy(fileName, help); while (GetFileAttributes(fileName) != -1) { index++; if (index < 10) zeroes = _CONV("0000"); else if (index < 100) zeroes = _CONV("000"); else if (index < 1000) zeroes = _CONV("00"); else if (index < 10000) zeroes = _CONV("0"); else zeroes = _CONV(""); //sprintf_s(fileName, MAX_PATH, "%s%s%s%s%d%s", drive, dir, fname, (LPCTSTR) zeroes.c_str(), index, ext); fileNameStr = string_format(_CONV("%s%s%s%s%d%s"), drive, dir, fname, (LPCTSTR)zeroes.c_str(), index, ext); help = fileNameStr.c_str(); type_copy(fileName, help); } // INDEX FOUND //pFreeFileName->Format("%s%s%s%s%d%s", drive, dir, fname, (LPCTSTR) zeroes.c_str(), index, ext); *pFreeFileName = string_format(_CONV("%s%s%s%s%d%s"), drive, dir, fname, (LPCTSTR)zeroes.c_str(), index, ext); return status; }
type *type_setdim(type **pt,targ_size_t dim) { type *t = *pt; type_debug(t); if (t->Tcount > 1) /* if other people pointing at t */ { type *tn; tn = type_copy(t); tn->Tcount++; type_free(t); t = tn; } t->Tflags &= ~TFsizeunknown; /* we have determined its size */ t->Tdim = dim; /* index of array */ return *pt = t; }
type *type_setty(type **pt,long newty) { type *t; t = *pt; type_debug(t); if ((tym_t)newty != t->Tty) { if (t->Tcount > 1) /* if other people pointing at t */ { type *tn; tn = type_copy(t); tn->Tcount++; type_free(t); t = tn; *pt = t; } t->Tty = newty; } return t; }
type *type_setmangle(type **pt,mangle_t mangle) { type *t; t = *pt; type_debug(t); if (mangle != type_mangle(t)) { if (t->Tcount > 1) // if other people pointing at t { type *tn; tn = type_copy(t); tn->Tcount++; type_free(t); t = tn; *pt = t; } t->Tmangle = mangle; } return t; }
// recursively copy all the way down. struct type * type_copy(struct type *t){ if(!t) return NULL; struct type * temp = type_create(t->kind, param_list_copy(t->params), type_copy(t->subtype), expr_copy(t->opt_expr)); return temp; }
struct type *expr_typecheck(struct expr *e) { if(!e) return type_create(TYPE_VOID, 0, 0, 0); struct param_list *param_ptr; struct expr *expr_ptr; struct type *left; struct type *right; switch(e->kind) { case EXPR_LIST: left = expr_typecheck(e->left); right = expr_typecheck(e->right); return type_create(TYPE_VOID, 0, 0, 0); break; case EXPR_ASSIGNMENT: left = expr_typecheck(e->left); right = expr_typecheck(e->right); while(left->kind == TYPE_ARRAY) left = left->subtype; while(right->kind == TYPE_ARRAY) left = left->subtype; if(type_equal(left, right) && left->kind != TYPE_FUNCTION) { return type_copy(left); } else { printf("Cannot assign "); type_print(right); printf(" to "); type_print(left); if(e->left->name) { printf(" %s\n", e->left->name); } else { printf("\n"); } type_check_errors++; return left; } break; case EXPR_NOT_EQUALS: case EXPR_EQUALS: left = expr_typecheck(e->left); right = expr_typecheck(e->right); if(type_equal(left, right) && left->kind != TYPE_FUNCTION && left->kind != TYPE_ARRAY) { return type_create(TYPE_BOOLEAN, 0, 0, 0); } else { printf("Cannot perform logical equals operation on "); type_print(left); printf(" and "); type_print(right); printf("\n"); type_check_errors++; return type_create(TYPE_BOOLEAN, 0, 0, 0); } break; case EXPR_LT: case EXPR_GT: case EXPR_LE: case EXPR_GE: left = expr_typecheck(e->left); right = expr_typecheck(e->right); if(left->kind == TYPE_INTEGER && right->kind == TYPE_INTEGER) { return type_create(TYPE_BOOLEAN, 0, 0, 0); } else { printf("Cannot perform boolean operations on "); type_print(left); printf(" and "); type_print(right); printf("\n"); type_check_errors++; return type_create(TYPE_BOOLEAN, 0, 0, 0); } break; case EXPR_ADD: case EXPR_MINUS: case EXPR_TIMES: case EXPR_DIVIDES: case EXPR_MOD: case EXPR_POWER: left = expr_typecheck(e->left); right = expr_typecheck(e->right); if(left->kind == TYPE_INTEGER && right->kind == TYPE_INTEGER) { return type_create(TYPE_INTEGER, 0, 0, 0); } else { printf("Cannot perform arithmetic operations on "); type_print(left); printf(" and "); type_print(right); printf("\n"); type_check_errors++; return type_create(TYPE_INTEGER, 0, 0, 0); } break; case EXPR_NEGATIVE: right = expr_typecheck(e->right); if(right->kind == TYPE_INTEGER) { return type_create(TYPE_INTEGER, 0, 0, 0); } else { printf("Cannot take the negative of "); type_print(right); printf("\n"); type_check_errors++; return type_create(TYPE_INTEGER, 0, 0, 0); } break; case EXPR_OR: case EXPR_AND: left = expr_typecheck(e->left); right = expr_typecheck(e->right); if(left->kind == TYPE_BOOLEAN && right->kind == TYPE_BOOLEAN) { return type_create(TYPE_BOOLEAN, 0, 0, 0); } else { printf("Cannot perform logical operations on "); type_print(left); printf(" and "); type_print(right); printf("\n"); type_check_errors++; return type_create(TYPE_BOOLEAN, 0, 0, 0); } break; case EXPR_NOT: right = expr_typecheck(e->right); if(right->kind == TYPE_BOOLEAN) { return type_create(TYPE_BOOLEAN, 0, 0, 0); } else { printf("Cannot perform a logical not on "); type_print(right); printf("\n"); type_check_errors++; return type_create(TYPE_BOOLEAN, 0, 0, 0); } break; case EXPR_PRE_INCREMENT: case EXPR_PRE_DECREMENT: right = expr_typecheck(e->right); if(right->kind == TYPE_INTEGER) { return type_create(TYPE_INTEGER, 0, 0, 0); } else { printf("Cannot perform integer operations on "); type_print(right); printf("\n"); type_check_errors++; return type_create(TYPE_INTEGER, 0, 0, 0); } break; case EXPR_POST_INCREMENT: case EXPR_POST_DECREMENT: left = expr_typecheck(e->left); if(left->kind == TYPE_INTEGER) { return type_create(TYPE_INTEGER, 0, 0, 0); } else { printf("Cannot perform integer operations on "); type_print(left); printf("\n"); type_check_errors++; return type_create(TYPE_INTEGER, 0, 0, 0); } break; case EXPR_FUNCTION: param_ptr = e->left->symbol->type->params; expr_ptr = e->right; while(param_ptr) { if(!expr_ptr) { printf("Not enough arguments given for function %s\n", e->left->name); type_check_errors++; break; } if(!type_equal(param_ptr->type, expr_typecheck(expr_ptr->left))) { printf("Function %s requires a paramater of type ", e->left->name); type_print(param_ptr->type); printf(" but "); expr_print(expr_ptr->left); printf(" is of type "); type_print(expr_typecheck(expr_ptr->left)); printf("\n"); type_check_errors++; break; } param_ptr = param_ptr->next; expr_ptr = expr_ptr->right; } if(expr_ptr) { printf("Too many arguments given for function %s\n", e->left->name); type_check_errors++; } return e->left->symbol->type->subtype; break; case EXPR_BOOLEAN: return type_create(TYPE_BOOLEAN, 0, 0, 0); break; case EXPR_INT: return type_create(TYPE_INTEGER, 0, 0, 0); break; case EXPR_CHAR: return type_create(TYPE_CHARACTER, 0, 0, 0); break; case EXPR_STRING: return type_create(TYPE_STRING, 0, 0, 0); break; case EXPR_NAME: if (e->symbol) return e->symbol->type; return type_create(TYPE_VOID, 0, 0, 0); break; case EXPR_ARRAY: left = expr_typecheck(e->left); right = expr_typecheck(e->right); if(right->kind == TYPE_INTEGER) { return type_create(left->subtype->kind, 0, 0, 0); } else { printf("Cannot use "); type_print(right); printf(" as an array index. Must use an integer\n"); type_check_errors++; return type_create(left->subtype->kind, 0, 0, 0); } break; case EXPR_ARRAY_LITERAL: left = expr_typecheck(e->right->left); return type_create(TYPE_ARRAY, 0, 0, left->subtype); break; } return type_create(TYPE_VOID, 0, 0, 0); }
struct type * expr_typecheck( struct expr *e ) { if (!e) return type_create(TYPE_VOID, 0, 0); struct type *L; struct type *R; switch (e->kind) { case EXPR_INT_VAL: return type_create(TYPE_INTEGER, 0, 0); case EXPR_NAME: return type_copy(e->symbol->type); case EXPR_STRING_VAL: return type_create(TYPE_STRING, 0, 0); case EXPR_CHAR_VAL: return type_create(TYPE_CHARACTER, 0, 0); case EXPR_BOOLEAN_VAL: return type_create(TYPE_BOOLEAN, 0, 0); case EXPR_ARRAY_VAL: { struct expr *vals = e->right; struct type *subtype = 0; int num_subtype = 0; while (vals) { struct type *cur_t = expr_typecheck(vals); if (!subtype) { subtype = cur_t; } if (!type_compare(subtype, cur_t)) { decl_has_error = 1; printf("type error: array of element type "); type_print(subtype); printf(" cannot have expression of type "); type_print(cur_t); printf("\n"); } num_subtype++; vals = vals->next; } struct type *t = type_create(TYPE_ARRAY, 0, subtype); t->num_subtype = expr_create_integer_literal(num_subtype); return t; } //return type_copy(e->symbol->type); case EXPR_ARRAY_SUB: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (R->kind != TYPE_INTEGER) { decl_has_error = 1; printf("type error: cannot use "); type_print(R); printf(" as index to an array\n"); } if (L->kind != TYPE_ARRAY) { decl_has_error = 1; printf("type error: access an "); type_print(R); printf(" as an array\n"); } return type_copy(L->subtype); case EXPR_FUNCTION_VAL: // need to check function params!!!!! param_list_typecheck(e->right, e->symbol->params, e->name); return type_copy(e->symbol->type->subtype); case EXPR_ADD: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind != TYPE_INTEGER || R->kind != TYPE_INTEGER) { decl_has_error = 1; printf("type error: cannot add "); type_print(L); printf(" to a "); type_print(R); printf("\n"); } return type_create(TYPE_INTEGER, 0, 0); case EXPR_SUB: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if ( !(L->kind == TYPE_INTEGER && L->kind == TYPE_INTEGER) || R->kind != TYPE_INTEGER) { decl_has_error = 1; printf("type error: cannot subtract "); type_print(L); printf(" to a "); type_print(R); printf("\n"); } return type_create(TYPE_INTEGER, 0, 0); case EXPR_MUL: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind != TYPE_INTEGER || R->kind != TYPE_INTEGER) { decl_has_error = 1; printf("type error: cannot multiply "); type_print(L); printf(" to a "); type_print(R); printf("\n"); } return type_create(TYPE_INTEGER, 0, 0); case EXPR_DIV: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind != TYPE_INTEGER || R->kind != TYPE_INTEGER) { decl_has_error = 1; printf("type error: cannot divide "); type_print(L); printf(" to a "); type_print(R); printf("\n"); } return type_create(TYPE_INTEGER, 0, 0); case EXPR_MODULO: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind != TYPE_INTEGER || R->kind != TYPE_INTEGER) { decl_has_error = 1; printf("type error: cannot module "); type_print(L); printf(" to a "); type_print(R); printf("\n"); } return type_create(TYPE_INTEGER, 0, 0); case EXPR_EXPO: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind != TYPE_INTEGER || R->kind != TYPE_INTEGER) { decl_has_error = 1; printf("type error: cannot expo "); type_print(L); printf(" to a "); type_print(R); printf("\n"); } return type_create(TYPE_INTEGER, 0, 0); case EXPR_POST_DECREASE: L = expr_typecheck(e->left); if (L->kind != TYPE_INTEGER ) { decl_has_error = 1; printf("type error: cannot decrease a "); type_print(L); printf("\n"); } return type_create(TYPE_INTEGER, 0, 0); case EXPR_POST_INCREASE: L = expr_typecheck(e->left); if (L->kind != TYPE_INTEGER ) { decl_has_error = 1; printf("type error: cannot add a "); type_print(L); printf("\n"); } return type_create(TYPE_INTEGER, 0, 0); case EXPR_GE: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind != TYPE_INTEGER || R->kind != TYPE_INTEGER) { decl_has_error = 1; printf("type error: cannot make greater equal than compare "); type_print(L); printf(" with a "); type_print(R); printf("\n"); } return type_create(TYPE_BOOLEAN, 0, 0); case EXPR_LE: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind != TYPE_INTEGER || R->kind != TYPE_INTEGER) { decl_has_error = 1; printf("type error: cannot make less equal compare "); type_print(L); printf(" with a "); type_print(R); printf("\n"); } return type_create(TYPE_BOOLEAN, 0, 0); case EXPR_GT: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind != TYPE_INTEGER || R->kind != TYPE_INTEGER) { decl_has_error = 1; printf("type error: cannot make greater than compare "); type_print(L); printf(" with a "); type_print(R); printf("\n"); } return type_create(TYPE_BOOLEAN, 0, 0); case EXPR_LT: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind != TYPE_INTEGER || R->kind != TYPE_INTEGER) { decl_has_error = 1; printf("type error: cannot make less than compare "); type_print(L); printf(" with a "); type_print(R); printf("\n"); } return type_create(TYPE_BOOLEAN, 0, 0); case EXPR_AND: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind != TYPE_BOOLEAN || R->kind != TYPE_BOOLEAN) { decl_has_error = 1; printf("type error: cannot and "); type_print(L); printf(" with a "); type_print(R); printf("\n"); } return type_create(TYPE_BOOLEAN, 0, 0); case EXPR_NOT: R = expr_typecheck(e->right); if (R->kind != TYPE_BOOLEAN) { decl_has_error = 1; printf("type error: cannot not a "); type_print(R); printf("\n"); } return type_create(TYPE_BOOLEAN, 0, 0); case EXPR_OR: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind != TYPE_BOOLEAN || R->kind != TYPE_BOOLEAN) { decl_has_error = 1; printf("type error: cannot or "); type_print(L); printf(" with a "); type_print(R); printf("\n"); } return type_create(TYPE_BOOLEAN, 0, 0); case EXPR_EQ: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind == TYPE_FUNCTION || R->kind == TYPE_FUNCTION || L->kind == TYPE_ARRAY || R->kind == TYPE_ARRAY) { decl_has_error = 1; printf("type error: cannot compare equality "); type_print(L); printf(" with a "); type_print(R); printf("\n"); } return type_create(TYPE_BOOLEAN, 0, 0); case EXPR_NEQ: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind == TYPE_FUNCTION || R->kind == TYPE_FUNCTION || L->kind == TYPE_ARRAY || R->kind == TYPE_ARRAY) { decl_has_error = 1; printf("type error: cannot compare non equality "); type_print(L); printf(" with a "); type_print(R); printf("\n"); } return type_create(TYPE_BOOLEAN, 0, 0); case EXPR_ASSIGN: L = expr_typecheck(e->left); R = expr_typecheck(e->right); if (L->kind == TYPE_FUNCTION || R->kind == TYPE_FUNCTION) { decl_has_error = 1; printf("type error: cannot assign "); type_print(R); printf(" to a "); type_print(L); printf("\n"); } if (!type_compare(L, R)) { decl_has_error = 1; printf("type error: cannot assign "); type_print(R); printf(" to a "); type_print(L); printf("\n"); } return type_copy(R); } }
list_t * list_set_type(list_t *list, type_t *type) { type_copy(&(list -> type), type); return list; }
struct param_list *param_list_copy(struct param_list *p) { if (!p) return NULL; struct param_list *new_param_list = param_list_create(strdup(p->name), type_copy(p->type), param_list_copy(p->next)); return new_param_list; }
struct param_list * param_list_copy(struct param_list *p){ if(!p) return NULL; struct param_list * temp = param_list_create(p->name, type_copy(p->type), param_list_copy(p->next)); temp->symbol = scope_lookup(p->name); return temp; }