semantic_analyser.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. #include "semantic_analyser.h"
  2. #include "lexer.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "symtable.h"
  7. #include "struct_table.h"
  8. #include "agc_commands.h"
  9. #include <stdarg.h>
  10. extern enum AVDL_PLATFORM avdl_platform;
  11. static struct ast_node *expect_command_definition();
  12. static struct ast_node *expect_command_classDefinition();
  13. static struct ast_node *expect_command_group();
  14. static struct ast_node *expect_command_functionDefinition();
  15. static struct ast_node *expect_command_classFunction();
  16. static struct ast_node *expect_identifier();
  17. static struct ast_node *expect_int();
  18. static struct ast_node *expect_float();
  19. static struct ast_node *expect_string();
  20. static struct ast_node *expect_command_binaryOperation();
  21. static struct ast_node *expect_command();
  22. static struct ast_node *expect_command_arg();
  23. static struct ast_node *expect_command_if();
  24. static struct ast_node *expect_command_include();
  25. static struct ast_node *expect_command_asset();
  26. static struct ast_node *expect_command_for();
  27. static struct ast_node *expect_command_multistring();
  28. static struct ast_node *expect_command_return();
  29. static void semantic_error(const char *msg, ...);
  30. static struct ast_node *expect_command_return() {
  31. struct ast_node *returncmd = ast_create(AST_COMMAND_NATIVE, 0);
  32. ast_addLex(returncmd, "return");
  33. if (lexer_peek() != LEXER_TOKEN_COMMANDEND) {
  34. ast_child_add(returncmd, expect_command_arg());
  35. }
  36. return returncmd;
  37. }
  38. static struct ast_node *expect_command_multistring() {
  39. struct ast_node *multistringcmd = ast_create(AST_COMMAND_NATIVE, 0);
  40. ast_addLex(multistringcmd, "multistring");
  41. while (lexer_peek() == LEXER_TOKEN_STRING) {
  42. ast_child_add(multistringcmd, expect_string());
  43. }
  44. return multistringcmd;
  45. }
  46. static struct ast_node *expect_command_for() {
  47. struct ast_node *definition = expect_command();
  48. struct ast_node *condition = expect_command();
  49. struct ast_node *step = expect_command();
  50. struct ast_node *statements = expect_command();
  51. struct ast_node *forcmd = ast_create(AST_COMMAND_NATIVE, 0);
  52. ast_addLex(forcmd, "for");
  53. ast_child_add(forcmd, definition);
  54. ast_child_add(forcmd, condition);
  55. ast_child_add(forcmd, step);
  56. ast_child_add(forcmd, statements);
  57. return forcmd;
  58. }
  59. static struct ast_node *expect_command_asset() {
  60. struct ast_node *asset = expect_string();
  61. /*
  62. * on android, a path to a file is truncated to the filename
  63. * minus the ending.
  64. *
  65. * temporary solution
  66. */
  67. if (avdl_platform == AVDL_PLATFORM_ANDROID) {
  68. char buffer[500];
  69. strcpy(buffer, asset->lex);
  70. char *lastSlash = buffer;
  71. char *p = buffer;
  72. while (p[0] != '\0') {
  73. if (p[0] == '/') {
  74. lastSlash = p+1;
  75. }
  76. p++;
  77. }
  78. char *lastDot = buffer;
  79. p = buffer;
  80. while (p[0] != '\0') {
  81. if (p[0] == '.') {
  82. lastDot = p;
  83. }
  84. p++;
  85. }
  86. lastDot[0] = '\0';
  87. strcpy(asset->lex, lastSlash);
  88. }
  89. // waste the type for now
  90. expect_identifier();
  91. return asset;
  92. }
  93. static struct ast_node *expect_command_include() {
  94. struct ast_node *include = ast_create(AST_INCLUDE, 0);
  95. struct ast_node *filename = expect_string();
  96. ast_delete(filename);
  97. ast_addLex(include, filename->lex);
  98. return include;
  99. }
  100. static struct ast_node *expect_command_if() {
  101. struct ast_node *ifcmd = ast_create(AST_COMMAND_NATIVE, 0);
  102. ast_addLex(ifcmd, "if");
  103. while (lexer_peek() != LEXER_TOKEN_COMMANDEND) {
  104. ast_child_add(ifcmd, expect_command_arg());
  105. }
  106. return ifcmd;
  107. }
  108. static struct ast_node *expect_command_classFunction() {
  109. struct ast_node *classFunc = ast_create(AST_COMMAND_NATIVE, 0);
  110. ast_addLex(classFunc, "class_function");
  111. struct ast_node *classname = expect_identifier();
  112. struct ast_node *function = expect_command_functionDefinition();
  113. symtable_push();
  114. struct entry *e = symtable_entryat(symtable_insert("this", DD_VARIABLE_TYPE_STRUCT));
  115. e->isRef = 1;
  116. e->value = struct_table_get_index(classname->lex);
  117. // function arguments
  118. struct ast_node *funcargs = dd_da_get(&function->children, 2);
  119. for (int i = 1; i < funcargs->children.elements; i += 2) {
  120. struct ast_node *type = dd_da_get(&funcargs->children, i-1);
  121. struct ast_node *name = dd_da_get(&funcargs->children, i);
  122. struct entry *symEntry = symtable_entryat(symtable_insert(name->lex, dd_variable_type_convert(type->lex)));
  123. if (!dd_variable_type_isPrimitiveType(type->lex)) {
  124. symEntry->isRef = 1;
  125. symEntry->value = struct_table_get_index(type->lex);
  126. }
  127. }
  128. //symtable_print();
  129. struct ast_node *functionStatements = expect_command();
  130. symtable_pop();
  131. ast_child_add(classFunc, classname);
  132. ast_child_add(function, functionStatements);
  133. ast_child_add(classFunc, function);
  134. return classFunc;
  135. }
  136. static struct ast_node *expect_command_functionDefinition() {
  137. struct ast_node *function = ast_create(AST_COMMAND_NATIVE, 0);
  138. ast_addLex(function, "function");
  139. struct ast_node *returnType = expect_identifier();
  140. struct ast_node *functionName = expect_identifier();
  141. struct ast_node *args = expect_command();
  142. ast_child_add(function, returnType);
  143. ast_child_add(function, functionName);
  144. ast_child_add(function, args);
  145. return function;
  146. }
  147. static struct ast_node *expect_command_group() {
  148. struct ast_node *group = ast_create(AST_COMMAND_NATIVE, 0);
  149. ast_addLex(group, "group");
  150. while (lexer_peek() != LEXER_TOKEN_COMMANDEND) {
  151. ast_child_add(group, expect_command_arg());
  152. }
  153. return group;
  154. }
  155. static struct ast_node *getIdentifierArrayNode(struct ast_node *n) {
  156. for (int i = 0; i < n->children.elements; i++) {
  157. struct ast_node *child = dd_da_get(&n->children, i);
  158. if (child->node_type == AST_GROUP) {
  159. return child;
  160. }
  161. }
  162. return 0;
  163. }
  164. static struct ast_node *expect_command_classDefinition() {
  165. struct ast_node *classname = expect_identifier();
  166. // subclass can be an identifier (name of the class) or `0` (no parent class)
  167. struct ast_node *subclassname;
  168. if (lexer_peek() == LEXER_TOKEN_IDENTIFIER) {
  169. subclassname = expect_identifier();
  170. }
  171. else {
  172. struct ast_node *n = expect_int();
  173. if (n->value != 0) {
  174. semantic_error("subclass can either be an identifier or '0'");
  175. }
  176. subclassname = 0;
  177. }
  178. symtable_push();
  179. struct ast_node *definitions = expect_command();
  180. int structIndex = struct_table_push(classname->lex, subclassname->lex);
  181. for (int i = 0; i < definitions->children.elements; i++) {
  182. struct ast_node *child = dd_da_get(&definitions->children, i);
  183. struct ast_node *type = dd_da_get(&child->children, 0);
  184. struct ast_node *name = dd_da_get(&child->children, 1);
  185. if (strcmp(child->lex, "def") == 0) {
  186. struct entry *e = symtable_entryat(symtable_lookup(name->lex));
  187. struct ast_node *arrayNode = getIdentifierArrayNode(name);
  188. //printf("variable: %s %s\n", type->lex, name->lex);
  189. if (arrayNode) {
  190. if (arrayNode->children.elements == 0) {
  191. semantic_error("array definition should have a value");
  192. }
  193. struct ast_node *arrayNum = dd_da_get(&arrayNode->children, 0);
  194. if (arrayNum->node_type != AST_NUMBER) {
  195. semantic_error("array definition should only be a number");
  196. }
  197. struct_table_push_member_array(name->lex, dd_variable_type_convert(type->lex), type->lex, arrayNum->value, e->isRef);
  198. }
  199. else {
  200. struct_table_push_member(name->lex, dd_variable_type_convert(type->lex), type->lex, e->isRef);
  201. }
  202. }
  203. else {
  204. //printf("function: %s %s\n", type->lex, name->lex);
  205. int parentDepth = struct_table_is_member_parent(structIndex, name->lex);
  206. //printf("var: %s %s\n", type->lex, name->lex);
  207. //printf("parent depth: %d\n", parentDepth);
  208. // override function
  209. if (parentDepth >= 0) {
  210. child->value = 1;
  211. }
  212. // new function
  213. else {
  214. }
  215. struct_table_push_member(name->lex, DD_VARIABLE_TYPE_FUNCTION, 0, 0);
  216. }
  217. }
  218. symtable_pop();
  219. /* scan definitions
  220. * if a function was defined in any of the subclasses, mark is
  221. * as an override
  222. */
  223. struct ast_node *classDefinition = ast_create(AST_COMMAND_NATIVE, 0);
  224. ast_addLex(classDefinition, "class");
  225. ast_child_add(classDefinition, classname);
  226. ast_child_add(classDefinition, subclassname);
  227. ast_child_add(classDefinition, definitions);
  228. return classDefinition;
  229. }
  230. static struct ast_node *expect_command_definition() {
  231. struct ast_node *definition = ast_create(AST_COMMAND_NATIVE, 0);
  232. ast_addLex(definition, "def");
  233. int isRef = 0;
  234. int isExtern = 0;
  235. // optional modifiers
  236. struct ast_node *optionalModifier = expect_identifier();
  237. do {
  238. if (strcmp(optionalModifier->lex, "ref") == 0) {
  239. // apply modifier
  240. isRef = 1;
  241. // get new optional modifier
  242. ast_delete(optionalModifier);
  243. optionalModifier = expect_identifier();
  244. }
  245. else
  246. if (strcmp(optionalModifier->lex, "extern") == 0) {
  247. // apply modifier
  248. isExtern = 1;
  249. // get new optional modifier
  250. ast_delete(optionalModifier);
  251. optionalModifier = expect_identifier();
  252. }
  253. else {
  254. break;
  255. }
  256. } while (1);
  257. definition->isExtern = isExtern;
  258. // get type
  259. struct ast_node *type = optionalModifier;
  260. // check if primitive or struct
  261. if (!dd_variable_type_isPrimitiveType(type->lex)
  262. && !struct_table_exists(type->lex)) {
  263. semantic_error("unrecognized type '%s'", type->lex);
  264. }
  265. // get variable name
  266. struct ast_node *varname = expect_identifier();
  267. // add newly defined variable to symbol table
  268. struct entry *e = symtable_entryat(symtable_insert(varname->lex, SYMTABLE_VARIABLE));
  269. e->value = dd_variable_type_convert(type->lex);
  270. e->isRef = isRef;
  271. definition->isRef = isRef;
  272. ast_child_add(definition, type);
  273. ast_child_add(definition, varname);
  274. if (lexer_peek() != LEXER_TOKEN_COMMANDEND) {
  275. struct ast_node *initialValue = expect_command_arg();
  276. ast_child_add(definition, initialValue);
  277. }
  278. return definition;
  279. }
  280. static struct ast_node *expect_int() {
  281. // confirm it's an integer
  282. if (lexer_getNextToken() != LEXER_TOKEN_INT) {
  283. semantic_error("expected integer instead of '%s'", lexer_getLexToken());
  284. }
  285. struct ast_node *integer = ast_create(AST_NUMBER, atoi(lexer_getLexToken()));
  286. return integer;
  287. }
  288. static struct ast_node *expect_float() {
  289. // confirm it's a float
  290. if (lexer_getNextToken() != LEXER_TOKEN_FLOAT) {
  291. semantic_error("expected float instead of '%s'", lexer_getLexToken());
  292. }
  293. struct ast_node *f = ast_create(AST_FLOAT, 0);
  294. f->fvalue = atof(lexer_getLexToken());
  295. return f;
  296. }
  297. static struct ast_node *expect_string() {
  298. // confirm it's a string
  299. if (lexer_getNextToken() != LEXER_TOKEN_STRING) {
  300. semantic_error("expected string instead of '%s'", lexer_getLexToken());
  301. }
  302. struct ast_node *str = ast_create(AST_STRING, 0);
  303. ast_addLex(str, lexer_getLexToken());
  304. return str;
  305. }
  306. static struct ast_node *expect_command_binaryOperation(const char *binaryOperationLex) {
  307. struct ast_node *binaryOperation = ast_create(AST_COMMAND_NATIVE, 0);
  308. ast_addLex(binaryOperation, binaryOperationLex);
  309. while (lexer_peek() != LEXER_TOKEN_COMMANDEND) {
  310. ast_child_add(binaryOperation, expect_command_arg());
  311. }
  312. return binaryOperation;
  313. }
  314. static struct ast_node *expect_identifier() {
  315. // confirm it's an identifier
  316. if (lexer_getNextToken() != LEXER_TOKEN_IDENTIFIER) {
  317. semantic_error("expected identifier instead of '%s'", lexer_getLexToken());
  318. }
  319. // generate ast node for it
  320. struct ast_node *identifier = ast_create(AST_IDENTIFIER, 0);
  321. ast_addLex(identifier, lexer_getLexToken());
  322. struct entry *symEntry = symtable_lookupEntry(identifier->lex);
  323. if (symEntry) {
  324. identifier->isRef = symEntry->isRef;
  325. identifier->value = symEntry->value;
  326. }
  327. // does it have an array modifier?
  328. if (lexer_peek() == LEXER_TOKEN_ARRAYSTART) {
  329. lexer_getNextToken();
  330. struct ast_node *array = ast_create(AST_GROUP, 0);
  331. int token = lexer_peek();
  332. // integer as array modifier
  333. if (token == LEXER_TOKEN_INT) {
  334. ast_child_add(array, expect_int());
  335. }
  336. else
  337. // identifier as array modifier
  338. if (token == LEXER_TOKEN_IDENTIFIER) {
  339. ast_child_add(array, expect_identifier());
  340. }
  341. else
  342. // calculation as array modifier
  343. if (token == LEXER_TOKEN_COMMANDSTART) {
  344. lexer_getNextToken();
  345. while (lexer_peek() == LEXER_TOKEN_COMMANDSTART) {
  346. ast_child_add(array, expect_command());
  347. }
  348. }
  349. ast_child_add(identifier, array);
  350. // end of array
  351. if (lexer_getNextToken() != LEXER_TOKEN_ARRAYEND) {
  352. semantic_error("expected end of array");
  353. }
  354. }
  355. // it has a period (myclass.myvar)
  356. if (lexer_peek() == LEXER_TOKEN_PERIOD) {
  357. lexer_getNextToken();
  358. //symtable_print();
  359. // identifiers that own objects have to be in the symbol table
  360. if (!symEntry) {
  361. semantic_error("identifier '%s' not a known symbol", identifier->lex);
  362. }
  363. // identifiers that own objects have to be structs
  364. if (symEntry->token != DD_VARIABLE_TYPE_STRUCT) {
  365. semantic_error("identifier '%s' not a struct, so it can't own objects");
  366. }
  367. //printf("name of struct: %s\n", struct_table_get_name(e->value));
  368. // add struct's members to new symbol table
  369. symtable_push();
  370. for (unsigned int j = 0; j < struct_table_get_member_total(symEntry->value); j++) {
  371. /*
  372. printf(" member: %s %d %s\n",
  373. struct_table_get_member_name(e->value, j),
  374. struct_table_get_member_type(e->value, j),
  375. struct_table_get_member_nametype(e->value, j)
  376. );
  377. */
  378. struct entry *e2 = symtable_entryat(symtable_insert(
  379. struct_table_get_member_name(symEntry->value, j),
  380. struct_table_get_member_type(symEntry->value, j))
  381. );
  382. if (struct_table_get_member_type(symEntry->value, j) == DD_VARIABLE_TYPE_STRUCT) {
  383. e2->value = struct_table_get_index(struct_table_get_member_nametype(symEntry->value, j));
  384. }
  385. e2->isRef = struct_table_getMemberIsRef(symEntry->value, j);
  386. }
  387. // get the owned object's name
  388. struct ast_node *child = expect_identifier();
  389. // child not inside current symbol table - possibly belongs to a parent class
  390. int childSymId = symtable_lookup(child->lex);
  391. if (childSymId < 0) {
  392. int parentDepth = struct_table_is_member_parent(symEntry->value, child->lex);
  393. // child not part of that struct, or any of its parent classes
  394. if (parentDepth < 0) {
  395. semantic_error("variable '%s' of class '%s' does not own object '%s'",
  396. identifier->lex, struct_table_get_name(symEntry->value), child->lex
  397. );
  398. }
  399. else
  400. // child direct child of that class, but not in symbol table, this should never happen
  401. if (parentDepth == 0) {
  402. semantic_error("variable '%s' of class '%s' owns object '%s', but couldn't be found in the symbol table",
  403. identifier->lex, struct_table_get_name(symEntry->value), child->lex
  404. );
  405. }
  406. // add "parent" children in ast, for each parent class depth level
  407. struct ast_node *lastChild = identifier;
  408. for (int i = 0; i < parentDepth; i++) {
  409. struct ast_node *parentChild = ast_create(AST_IDENTIFIER, 0);
  410. ast_addLex(parentChild, "parent");
  411. int childIndex = ast_child_add(lastChild, parentChild);
  412. lastChild = dd_da_get(&lastChild->children, childIndex);
  413. }
  414. // finally add the found child on the last "parent" node added
  415. ast_child_add(lastChild, child);
  416. }
  417. // child is directly owned by this struct, just add it as child
  418. else {
  419. if (struct_table_get_member_type(symEntry->value, struct_table_get_member(symEntry->value, child->lex)) == DD_VARIABLE_TYPE_STRUCT) {
  420. child->value = DD_VARIABLE_TYPE_STRUCT;
  421. }
  422. ast_child_add(identifier, child);
  423. }
  424. symtable_pop();
  425. }
  426. return identifier;
  427. }
  428. static struct ast_node *expect_command_arg() {
  429. int token = lexer_peek();
  430. if (token == LEXER_TOKEN_INT) {
  431. return expect_int();
  432. }
  433. else
  434. if (token == LEXER_TOKEN_FLOAT) {
  435. return expect_float();
  436. }
  437. else
  438. if (token == LEXER_TOKEN_STRING) {
  439. return expect_string();
  440. }
  441. else
  442. if (token == LEXER_TOKEN_COMMANDSTART) {
  443. return expect_command();
  444. }
  445. else
  446. if (token == LEXER_TOKEN_IDENTIFIER) {
  447. return expect_identifier();
  448. }
  449. else {
  450. lexer_getNextToken();
  451. semantic_error("expected command argument instead of '%s'", lexer_getLexToken());
  452. }
  453. return 0;
  454. }
  455. // looks for the structure of a command: (cmdname arg1 arg2 .. argN)
  456. static struct ast_node *expect_command() {
  457. // confirm that a command is expected
  458. if (lexer_getNextToken() != LEXER_TOKEN_COMMANDSTART) {
  459. semantic_error("expected the start of a command '(', instead of '%s'", lexer_getLexToken());
  460. }
  461. // get the command's name
  462. struct ast_node *cmd;
  463. struct ast_node *cmdname = expect_identifier();
  464. // native command, special rules
  465. if (agc_commands_isNative(cmdname->lex)) {
  466. // native command can only be a name, no array modifiers or owning data
  467. if (cmdname->children.elements > 0) {
  468. semantic_error("native command name cannot have an array modifier, or own other data\n");
  469. }
  470. // based on the command, expect different data
  471. if (strcmp(cmdname->lex, "def") == 0) {
  472. cmd = expect_command_definition();
  473. }
  474. else
  475. if (strcmp(cmdname->lex, "class") == 0) {
  476. cmd = expect_command_classDefinition();
  477. }
  478. else
  479. if (strcmp(cmdname->lex, "group") == 0) {
  480. cmd = expect_command_group();
  481. }
  482. else
  483. if (strcmp(cmdname->lex, "function") == 0) {
  484. cmd = expect_command_functionDefinition();
  485. if (lexer_peek() == LEXER_TOKEN_COMMANDSTART) {
  486. // function statements
  487. ast_child_add(cmd, expect_command());
  488. }
  489. }
  490. else
  491. if (strcmp(cmdname->lex, "class_function") == 0) {
  492. cmd = expect_command_classFunction();
  493. }
  494. else
  495. if (strcmp(cmdname->lex, "=") == 0
  496. || strcmp(cmdname->lex, "+") == 0
  497. || strcmp(cmdname->lex, "-") == 0
  498. || strcmp(cmdname->lex, "*") == 0
  499. || strcmp(cmdname->lex, "/") == 0
  500. || strcmp(cmdname->lex, "%") == 0
  501. || strcmp(cmdname->lex, "&&") == 0
  502. || strcmp(cmdname->lex, "||") == 0
  503. || strcmp(cmdname->lex, "==") == 0
  504. || strcmp(cmdname->lex, ">=") == 0
  505. || strcmp(cmdname->lex, "<=") == 0
  506. || strcmp(cmdname->lex, ">") == 0
  507. || strcmp(cmdname->lex, "<") == 0) {
  508. cmd = expect_command_binaryOperation(cmdname->lex);
  509. }
  510. else
  511. if (strcmp(cmdname->lex, "echo") == 0) {
  512. cmd = expect_command_group();
  513. ast_addLex(cmd, "echo");
  514. }
  515. else
  516. if (strcmp(cmdname->lex, "if") == 0) {
  517. cmd = expect_command_if();
  518. }
  519. else
  520. if (strcmp(cmdname->lex, "include") == 0) {
  521. cmd = expect_command_include();
  522. }
  523. else
  524. if (strcmp(cmdname->lex, "asset") == 0) {
  525. cmd = expect_command_asset();
  526. }
  527. else
  528. if (strcmp(cmdname->lex, "for") == 0) {
  529. cmd = expect_command_for();
  530. }
  531. else
  532. if (strcmp(cmdname->lex, "multistring") == 0) {
  533. cmd = expect_command_multistring();
  534. }
  535. else
  536. if (strcmp(cmdname->lex, "return") == 0) {
  537. cmd = expect_command_return();
  538. }
  539. else {
  540. semantic_error("no rule to parse command '%s'", cmdname->lex);
  541. }
  542. }
  543. // custom command, make sure it exists
  544. else {
  545. /*
  546. if (symtable_lookup(cmdname->lex) == -1) {
  547. printf("unrecognized identifier: %s\n", cmdname->lex);
  548. exit(-1);
  549. }
  550. */
  551. cmd = ast_create(0, 0);
  552. cmd->node_type = AST_COMMAND_CUSTOM;
  553. ast_child_add(cmd, cmdname);
  554. while (lexer_peek() != LEXER_TOKEN_COMMANDEND) {
  555. ast_child_add(cmd, expect_command_arg());
  556. }
  557. }
  558. // get the command's children
  559. if (lexer_getNextToken() != LEXER_TOKEN_COMMANDEND) {
  560. semantic_error("expected command end ')'");
  561. }
  562. return cmd;
  563. }
  564. void semanticAnalyser_convertToAst(struct ast_node *node, const char *filename) {
  565. struct_table_init();
  566. symtable_init();
  567. lexer_prepare(filename);
  568. struct ast_node *cmd;
  569. while (lexer_peek() == LEXER_TOKEN_COMMANDSTART) {
  570. cmd = expect_command();
  571. if (cmd->node_type == AST_INCLUDE) {
  572. lexer_addIncludedFile(cmd->lex);
  573. }
  574. else {
  575. ast_child_add(node, cmd);
  576. }
  577. }
  578. lexer_clean();
  579. }
  580. static void semantic_error(const char *msg, ...) {
  581. va_list args;
  582. va_start(args, msg);
  583. printf("avdl syntax error:\n");
  584. printf("%s:%d: ", lexer_getCurrentFilename(), lexer_getCurrentLinenumber());
  585. vprintf(msg, args);
  586. printf("\n");
  587. lexer_printCurrentLine();
  588. exit(-1);
  589. }