struct vars *parse_vars() { struct vars *v; v = malloc(sizeof(struct vars)); if (v == NULL) { perror("No memory"); return NULL; } v->decl = parse_decl(); if (v->decl == NULL) { free(v); return NULL; } if (get_token().type != T_SEP) { unget_token(); v->v = NULL; return v; } v->v = parse_vars(); if (v->v == NULL) { unget_token(); } return v; }
static Tree_s *pair (void) { FN; Tree_s *tree; Token_s s; Token_s t; Tree_s *v; Tree_s *p; s = get_token(); if (s.type != T_STRING) { unget_token(s); return NULL; } t = get_token(); if (t.type != ':') { unget_token(t); warn("Expecting ':'"); return NULL; } v = value(); if (!v) { warn("Expecting value"); return NULL; } tree = new_tree(); tree->token = s; tree->right = v; p = new_tree(); p->token.type = T_PAIR; p->left = tree; return p; }
static funcdef_t* parse_funcdef(parser_t* p, bool is_inline) { funcdef_t* funcdef = (funcdef_t*)malloc(sizeof(funcdef_t)); funcdef->line_number = p->t->line_number; match(p, TT_DEF); if (!is_inline) { match(p, TT_IDENT); strcpy(funcdef->name, ((char*)(p->t->token_value))); } funcdef->parameters = create_list(); token_type_t tok = get_token(p->t); if (TT_OP_POPEN == tok) { tok = get_token(p->t); if (TT_OP_PCLOSE != tok) { unget_token(p->t); do { list_insert(funcdef->parameters, parse_vardecl(p)); get_token(p->t); } while (TT_OP_COMMA == p->t->token_type); } expect(p, TT_OP_PCLOSE); } else { unget_token(p->t); } funcdef->block = parse_block(p); match(p, TT_END); return funcdef; }
double parse_expression() { double v1; double v2; Token token; v1 = parse_term(); for (;;) { my_get_token(&token); if (token.kind != ADD_OPERATOR_TOKEN && token.kind != SUB_OPERATOR_TOKEN) { unget_token(&token); break; } v2 = parse_term(); if (token.kind == ADD_OPERATOR_TOKEN) { v1 += v2; } else if (token.kind == SUB_OPERATOR_TOKEN) { v1 -= v2; } else { unget_token(&token); } } return v1; }
struct instruction *parse_instruction() { struct instruction *i; i = malloc(sizeof(struct instruction)); if (i == NULL) { perror("No memory"); return NULL; } i->s = parse_single(); if (i->s == NULL) { free(i); return NULL; } if (get_token().type != T_SEP) { unget_token(); i->i = NULL; return i; } i->i = parse_instruction(); if (i->i == NULL) { unget_token(); } return i; }
static double parse_primary_expression() { Token token; double value = 0.0; int minus_flag = 0; my_get_token(&token); if(token.kind == SUB_OPERATOR_TOKEN) { minus_flag = 1; } else { unget_token(&token); } my_get_token(&token); if(token.kind == NUMBER_TOKEN) { return token.value; } else if(token.kind == LEFT_PAREN_TOKEN) { value = parse_expression(); my_get_token(&token); if(token.kind != RIGHT_PAREN_TOKEN) { fprintf(stderr, "missing ')' error.\n"); exit(1); } } else { unget_token(&token); } if(minus_flag) { value = -value; } return value; }
void parse(void) { Token t; for (;;) { parse_expression(); t = get_token(); t = get_token(); if (t.type == T_EOF) { return; } unget_token();unget_token(); } }
struct program *parse_program() { struct program *p; if (get_token().type != T_HAI) { unget_token(); return NULL; } if (get_token().type != T_SEP) { unget_token(); parse_error("expected separator"); return NULL; } p = malloc(sizeof(struct program)); if (p == NULL) { perror("No memory"); return NULL; } p->v = parse_vars(); if (p->v != NULL) if (get_token().type != T_SEP) { parse_error("expected separator"); free(p); return NULL; } p->i = parse_instruction(); while (get_token().type == T_SEP); unget_token(); if (tokens[current_token].type != T_EOF) { parse_error("expecting instruction"); return NULL; } return p; }
void parse(parser_t* p) { token_type_t tok = get_token(p->t); while (TT_EOF != tok) { if (TT_DEF == tok) { unget_token(p->t); list_insert(p->ast->function_list, parse_funcdef(p, false)); } else { unget_token(p->t); statement_t* statement = parse_statement(p); list_insert(p->ast->statement_list, statement); } tok = get_token(p->t); } }
static statement_t* parse_statement(parser_t* p) { statement_t* statement = (statement_t*)malloc(sizeof(statement_t)); token_type_t tok = get_token(p->t); unget_token(p->t); switch (tok) { case TT_IF: statement->type = ST_IF; statement->value = parse_if(p); break; case TT_WHILE: statement->type = ST_WHILE; statement->value = parse_while(p); break; case TT_RETURN: statement->type = ST_RETURN; match(p, TT_RETURN); statement->value = parse_expression(p); break; case TT_PRINT: statement->type = ST_PRINT; match(p, TT_PRINT); statement->value = parse_expression(p); break; default: statement->type = ST_EXPRESSION; statement->value = parse_expression(p); } return statement; }
static Tree_s *array (void) { FN; Tree_s *tree = NULL; Token_s t; Tree_s *v; Tree_s *a; for (;;) { t = get_token(); if (t.type == ']') break; unget_token(t); v = value(); if (!v) break; if (tree) { tree->right = v; } else { tree = v; } t = get_token(); if (t.type == ',') continue; } a = new_tree(); a->token.type = T_ARRAY; a->left = tree; return a; }
void parse_item(void) { if (!gInItems) { fprintf(stderr, "\\item found outside an items environment\n"); print_page_and_filename(); print_next_ten_tokens(); jump(); } curr_node->type = Item; get_token(); if (token.type == Lsquarebrace) { /* I should parse the optional argument */ curr_node->next = alloc_node(); curr_node = curr_node->next; curr_node->type = Description; curr_node->next = alloc_node(); curr_node = curr_node->next; gInOptional++; parse_HyperDoc(); gInOptional--; curr_node->type = Enddescription; if (token.type != Rsquarebrace) { fprintf(stderr, "(HyperDoc) Optional arguments must end with ].\n"); print_next_ten_tokens(); print_page_and_filename(); jump(); } } else { unget_token(); } }
void parse_expression(void) { Token t; parse_term(); for(;;) { t = get_token(); if (t.type != T_ADD && t.type != T_SUB && t.type != T_OR && t.type != T_AND && t.type != T_XOR) { unget_token(); break; } parse_term(); Opcode op = OP_ADD; if (t.type == T_ADD) { op = OP_ADD; } else if (t.type == T_SUB) { op = OP_SUB; } else if (t.type == T_OR) { op = OP_OR; } else if (t.type == T_AND) { op = T_AND; } else if (t.type == T_XOR) { op = T_XOR; } vm_add_opcode(op); } }
void parse_begin_items(void) { TextNode *bi = curr_node; /* * This procedure parses a begin item. It sets the current * node and sees if there is an optional argument for the itemspace */ bi->type = token.type; get_token(); if (token.type == Lsquarebrace) { bi->data.node = alloc_node(); curr_node = bi->data.node; gInOptional++; parse_HyperDoc(); gInOptional--; curr_node->type = Enddescription; if (token.type != Rsquarebrace) { fprintf(stderr, "(HyperDoc) Optional arguments must end with ].\n"); print_next_ten_tokens(); print_page_and_filename(); jump(); } curr_node = bi; } else unget_token(); gInItems++; }
int declaration(char* result) { char token[MAXTOKEN]; char token2[MAXTOKEN]; bool is_const = false; int type = -1; int ret = 0; ret -= try_tokens_until(&type, token, try_declaration, try_declarator, "Missing data type to form a declaration"); if (type == NAME) { if (gettoken(token2) == CONST) { // T const XXX is_const = true; } else { unget_token(); } } else if (type == CONST) { is_const = true; ret -= try_tokens_until(&type, token, try_name, try_declarator, "Missing data type to form a declaration"); } ret += declarator(result); strcat(result, " "); if (is_const) { strcat(result, "const "); } strcat(result, token); return ret; }
static Tree_s *value (void) { FN; Token_s t; Tree_s *tree; t = get_token(); switch (t.type) { case T_STRING: case T_NUMBER: case T_TRUE: case T_FALSE: case T_NULL: tree = new_tree(); tree->token = t; return tree; case '[': return array(); case '{': unget_token(t); return object(); default: PRd(t.type); return NULL; } }
void parse_msg() { Token token; my_get_token(&token); char cls_name[MAX_TOKEN_SIZE]; if (token.kind == STRING_TOKEN) { strcpy(cls_name, token.str); my_get_token(&token); if (token.kind == LEFT_PAREN_TOKEN) { my_get_token(&token); char title[1024]; sprintf(title, "LCPB_%s = class(\"LCPB_%s\", LCProtoBase)\nfunction LCPB_%s:ctor(msgInput)\n\tlocal msg = msgInput or {}\n\tLCPB_%s.super.ctor(self, protoPack, \"%s\", msg)\n\tself.print = function()\n", cls_name, cls_name, cls_name, cls_name, cls_name); fputs(title, s_out_file); if (token.kind == RIGHT_PAREN_TOKEN) { fputs("\tend\nend\n\n", s_out_file); return; } else { while (token.kind != RIGHT_PAREN_TOKEN) { unget_token(&token); parse_expression(); my_get_token(&token); } fputs("\tend\nend\n\n", s_out_file); return; } } else { printf("no { next to cls name.\n"); return; } } else { printf("no cls name next to message.\n"); return; } }
int its_function() { int answer; htab_item *item; answer = not_function; token = get_token(); switch (token->type) { case TOKEN_IDENTIFIER: item = htab_lookup(G.g_globalTab, token->data); if (item == NULL) { find_var(token, 0); } else { answer = external_function; } break; case TOKEN_LENGTH: case TOKEN_SUBSTR: case TOKEN_CONCAT: case TOKEN_FIND: case TOKEN_SORT: answer = internal_function; } unget_token(token); return answer; }
static expression_t* parse_expression(parser_t* p) { expression_t* expression = (expression_t*)malloc(sizeof(expression_t)); expression->values = create_list(); expression->binaryops = create_list(); expression->unaryops = create_list(); expression->line_number = p->t->line_number; while (1) { token_type_t tok = get_token(p->t); if (tok == TT_OP_POPEN) { value_t* value = (value_t*)malloc(sizeof(value_t)); value->type = VT_EXPRESSION; value->value = parse_expression(p); value->subvalue = 0; list_insert(expression->values, value); match(p, TT_OP_PCLOSE); } else { if (TOK_IS_UNARY_OP(tok)) { if (tok == TT_OP_SUB) { tok = TT_OP_UNARYSUB; } list_insert(expression->unaryops, (void*)tok); } else { list_insert(expression->unaryops, (void*)TT_NOP); unget_token(p->t); } list_insert(expression->values, parse_value(p)); } tok = get_token(p->t); if (TOK_IS_BINARY_OP(tok)) { list_insert(expression->binaryops, (void*)tok); if (TT_OP_ASSIGN == tok) { value_t* value = (value_t*)malloc(sizeof(value_t)); value->type = VT_EXPRESSION; value->value = parse_expression(p); value->subvalue = 0; list_insert(expression->values, value); break; } } else { unget_token(p->t); break; } } return expression; }
static block_t* parse_block(parser_t* p) { block_t* block = (block_t*)malloc(sizeof(block_t)); block->statements = create_list(); while (1) { token_type_t tok; tok = get_token(p->t); if (IS_END_OF_BLOCK_TOKEN(tok)) { unget_token(p->t); break; } else { unget_token(p->t); list_insert(block->statements, parse_statement(p)); } } return block; }
void before_start() { printf("expr: --START--\n"); printf("expr: hint TYPE[VALUE]\n"); token = get_token(); printf("expr: First token "); print_token(token); printf("\n"); unget_token(token); }
void parse_split(void) { Token t = get_token(); if (t.type == T_SPLIT) { vm_add_opcode(OP_PRINT); parse_expression(); return; } else { unget_token(); parse_number(); } }
int declarator(char* result) { char tmp[MAXTOKEN] = ""; char token[MAXTOKEN]; for (int type = gettoken(token); type == '*'; type = gettoken(token)) { type = gettoken(token); if (type == CONST) { strcat(tmp, " const pointer to"); } else { strcat(tmp, " pointer to"); unget_token(); } } unget_token(); int ret = direct_declarator(result); strcat(result, tmp); return ret; }
struct single *parse_io() { token t; int current = current_token, error = 0; struct single *s; s = malloc(sizeof (struct single)); t = get_token(); if (t.type == T_VISIBLE) { if ((t = get_token()).type == T_STRING) { s->type = I_VISIBLE_STRING; s->data.string = t.data; } else { unget_token(); s->type = I_VISIBLE; s->data.e = parse_expr(); if (s->data.e == NULL) { error = 1; parse_error("I can only visible strings and epressions"); } } } else if (t.type == T_GIMMEH) { s->type = I_GIMMEH; s->data.id = parse_id(); if (s->data.id == NULL) { error = 1; } } else error = 1; if (error == 1) { free(s); current_token = current; return NULL; } return s; }
char *parse_id() { token t; t = get_token(); if (t.type != T_ID) { unget_token(); return NULL; } return (char *) t.data; }
int *parse_number() { token t; t = get_token(); if (t.type != T_NUMBER) { unget_token(); return NULL; } return (int *) t.data; }
void parse_primary_expression(void) { Token t = get_token(); if (t.type == T_OPEN_BRACKET) { parse_expression(); t = get_token(); if (t.type != T_CLOSE_BRACKET) { fprintf(stderr, "Syntax Error: Mismatch Bracket\n"); exit(-1); } } else { unget_token(); parse_split(); } }
struct vardecl *parse_decl() { int current = current_token; token t1, t2; struct vardecl *d; t1 = get_token(); t2 = get_token(); if (t1.type != T_I || t2.type != T_HAS) { current_token = current; return NULL; } d = malloc(sizeof(struct vardecl)); if (d == NULL) { perror("No memory"); return NULL; } d->id = parse_id(); if (d->id == NULL) { free(d); parse_error("expected id"); return NULL; } if (get_token().type != T_ITZ) { unget_token(); d->number = NULL; return d; } d->number = parse_number(); if (d->number == NULL) { free(d); parse_error("expected number"); return NULL; } return d; }
void parse_mod(void) { Token t; parse_pow(); for (;;) { t = get_token(); if (t.type != T_MOD) { unget_token(); break; } parse_mod(); if (t.type == T_MOD) { vm_add_opcode(OP_MOD); } } }
void parse_pow (void) { Token t; parse_primary_expression(); for (;;) { t = get_token(); if (t.type != T_POW) { unget_token(); break; } parse_pow(); if (t.type == T_POW) { vm_add_opcode(OP_POW); } } }