Compiler.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. package me.hammerle.snuviscript.code;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Stack;
  5. import me.hammerle.snuviscript.inputprovider.InputProvider;
  6. import me.hammerle.snuviscript.inputprovider.ConstantBoolean;
  7. import me.hammerle.snuviscript.inputprovider.ConstantDouble;
  8. import me.hammerle.snuviscript.inputprovider.ConstantNull;
  9. import me.hammerle.snuviscript.inputprovider.ConstantString;
  10. import me.hammerle.snuviscript.exceptions.PreScriptException;
  11. import me.hammerle.snuviscript.tokenizer.Token;
  12. import me.hammerle.snuviscript.tokenizer.TokenType;
  13. import static me.hammerle.snuviscript.tokenizer.TokenType.*;
  14. import me.hammerle.snuviscript.inputprovider.LocalVariable;
  15. import me.hammerle.snuviscript.inputprovider.Variable;
  16. import me.hammerle.snuviscript.instructions.Array;
  17. import me.hammerle.snuviscript.instructions.Break;
  18. import me.hammerle.snuviscript.instructions.Catch;
  19. import me.hammerle.snuviscript.instructions.Constant;
  20. import me.hammerle.snuviscript.instructions.Continue;
  21. import me.hammerle.snuviscript.instructions.Else;
  22. import me.hammerle.snuviscript.instructions.ElseIf;
  23. import me.hammerle.snuviscript.instructions.EndIf;
  24. import me.hammerle.snuviscript.instructions.For;
  25. import me.hammerle.snuviscript.instructions.Function;
  26. import me.hammerle.snuviscript.instructions.Goto;
  27. import me.hammerle.snuviscript.instructions.If;
  28. import me.hammerle.snuviscript.instructions.IfGoto;
  29. import me.hammerle.snuviscript.instructions.Instruction;
  30. import me.hammerle.snuviscript.instructions.Return;
  31. import me.hammerle.snuviscript.instructions.Try;
  32. import me.hammerle.snuviscript.instructions.UserFunction;
  33. import me.hammerle.snuviscript.instructions.While;
  34. public class Compiler
  35. {
  36. private int index = 0;
  37. private Token[] tokens = null;
  38. private final ArrayList<Instruction> instr = new ArrayList<>();
  39. private HashMap<String, Integer> labels = null;
  40. private HashMap<String, HashMap<String, Integer>> localLabels = null;
  41. private HashMap<String, Variable> vars = null;
  42. private final HashMap<String, LocalVariable> localVars = new HashMap<>();
  43. private HashMap<String, Integer> functions = null;
  44. private final Stack<Break> breakStack = new Stack<>();
  45. private final Stack<Continue> continueStack = new Stack<>();
  46. private String inFunction = null;
  47. private boolean lineExpression = false;
  48. private void addConstant(int line, InputProvider ip)
  49. {
  50. instr.add(new Constant(line, ip));
  51. }
  52. private void addFunction(int line, int args, String name)
  53. {
  54. instr.add(new Function(line, args, FunctionRegistry.getFunction(name)));
  55. }
  56. private void addGoto(int line, int jump)
  57. {
  58. Goto g = new Goto(line, 0);
  59. g.setJump(jump);
  60. instr.add(g);
  61. }
  62. private boolean match(TokenType... types)
  63. {
  64. for(TokenType type : types)
  65. {
  66. if(check(type))
  67. {
  68. advance();
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. private boolean check(TokenType type)
  75. {
  76. if(isAtEnd())
  77. {
  78. return false;
  79. }
  80. return peek().getType() == type;
  81. }
  82. private Token advance()
  83. {
  84. if(!isAtEnd())
  85. {
  86. index++;
  87. }
  88. return previous();
  89. }
  90. private boolean isAtEnd()
  91. {
  92. return peek().getType() == EOF;
  93. }
  94. private Token peek()
  95. {
  96. return tokens[index];
  97. }
  98. private Token previous()
  99. {
  100. return tokens[index - 1];
  101. }
  102. private Token consume(TokenType type)
  103. {
  104. if(check(type))
  105. {
  106. return advance();
  107. }
  108. throw new PreScriptException(String.format("expected %s got %s", type, peek().getType()), peek().getLine());
  109. }
  110. private void noReturnForLastFunction()
  111. {
  112. instr.get(instr.size() - 1).setNoReturn();
  113. }
  114. public Instruction[] compile(Token[] tokens, HashMap<String, Integer> labels,
  115. HashMap<String, Variable> vars, HashMap<String, Integer> functions,
  116. HashMap<String, HashMap<String, Integer>> localLabels)
  117. {
  118. this.tokens = tokens;
  119. index = 0;
  120. instr.clear();
  121. this.labels = labels;
  122. this.localLabels = localLabels;
  123. this.vars = vars;
  124. this.functions = functions;
  125. localVars.clear();
  126. inFunction = null;
  127. while(!isAtEnd())
  128. {
  129. line();
  130. }
  131. this.tokens = null;
  132. this.labels = null;
  133. this.vars = null;
  134. this.functions = null;
  135. localVars.clear();
  136. Instruction[] code = instr.toArray(new Instruction[instr.size()]);
  137. instr.clear();
  138. return code;
  139. }
  140. private void line()
  141. {
  142. int oldIndex = index;
  143. Token t = advance();
  144. switch(t.getType())
  145. {
  146. case LABEL: handleLabel(); break;
  147. case IF: handleIf(); break;
  148. case SEMICOLON: break;
  149. case FOR: handleFor(); break;
  150. case BREAK:
  151. Break b = new Break(previous().getLine());
  152. breakStack.add(b);
  153. instr.add(b);
  154. consume(SEMICOLON);
  155. break;
  156. case CONTINUE:
  157. Continue c = new Continue(previous().getLine());
  158. continueStack.add(c);
  159. instr.add(c);
  160. consume(SEMICOLON);
  161. break;
  162. case FUNCTION: handleUserFunction(); break;
  163. case RETURN: handleReturn(); break;
  164. case WHILE: handleWhile(); break;
  165. case TRY: handleTry(); break;
  166. default:
  167. index = oldIndex;
  168. lineExpression = false;
  169. expression();
  170. if(!lineExpression)
  171. {
  172. throw new PreScriptException("missing statement", t.getLine());
  173. }
  174. consume(SEMICOLON);
  175. }
  176. noReturnForLastFunction();
  177. }
  178. private void handleLabel()
  179. {
  180. String name = previous().getData().toString();
  181. name = name.substring(1); // cut off @ at start
  182. if(inFunction != null)
  183. {
  184. HashMap<String, Integer> llabel = localLabels.get(inFunction);
  185. if(llabel == null)
  186. {
  187. llabel = new HashMap<>();
  188. localLabels.put(inFunction, llabel);
  189. }
  190. llabel.put(name, instr.size() - 1);
  191. }
  192. else
  193. {
  194. labels.put(name, instr.size() - 1);
  195. }
  196. }
  197. private void handleIf()
  198. {
  199. Token t = previous();
  200. consume(OPEN_BRACKET);
  201. expression();
  202. If i = new If(t.getLine());
  203. instr.add(i);
  204. consume(CLOSE_BRACKET);
  205. consume(OPEN_CURVED_BRACKET);
  206. while(!match(CLOSE_CURVED_BRACKET))
  207. {
  208. line();
  209. }
  210. i.setJump(instr.size() - 1);
  211. handleElseIf();
  212. instr.add(new EndIf(instr.get(instr.size() - 1).getLine()));
  213. }
  214. private void handleElseIf()
  215. {
  216. while(match(ELSEIF))
  217. {
  218. Token t = previous();
  219. consume(OPEN_BRACKET);
  220. expression();
  221. ElseIf e = new ElseIf(t.getLine());
  222. instr.add(e);
  223. consume(CLOSE_BRACKET);
  224. consume(OPEN_CURVED_BRACKET);
  225. while(!match(CLOSE_CURVED_BRACKET))
  226. {
  227. line();
  228. }
  229. e.setJump(instr.size() - 1);
  230. }
  231. handleElse();
  232. }
  233. private void handleElse()
  234. {
  235. if(match(ELSE))
  236. {
  237. Else e = new Else(previous().getLine());
  238. instr.add(e);
  239. consume(OPEN_CURVED_BRACKET);
  240. while(!match(CLOSE_CURVED_BRACKET))
  241. {
  242. line();
  243. }
  244. e.setJump(instr.size() - 1);
  245. }
  246. }
  247. private void handleFor()
  248. {
  249. Token t = previous();
  250. consume(OPEN_BRACKET);
  251. if(!match(SEMICOLON))
  252. {
  253. expression();
  254. consume(SEMICOLON);
  255. noReturnForLastFunction();
  256. }
  257. int forConditionStart = instr.size() - 1;
  258. if(!match(SEMICOLON))
  259. {
  260. expression();
  261. consume(SEMICOLON);
  262. }
  263. Goto forGoto = new Goto(instr.get(instr.size() - 1).getLine(), 0);
  264. instr.add(forGoto);
  265. int forLoopFunctionStart = instr.size() - 1;
  266. if(!match(CLOSE_BRACKET))
  267. {
  268. expression();
  269. consume(CLOSE_BRACKET);
  270. noReturnForLastFunction();
  271. }
  272. Goto conditionGoto = new Goto(instr.get(instr.size() - 1).getLine(), 0);
  273. conditionGoto.setJump(forConditionStart);
  274. instr.add(conditionGoto);
  275. int forStart = instr.size() - 1;
  276. forGoto.setJump(forStart);
  277. For f = new For(t.getLine());
  278. instr.add(f);
  279. consume(OPEN_CURVED_BRACKET);
  280. while(!match(CLOSE_CURVED_BRACKET))
  281. {
  282. line();
  283. }
  284. Goto loopFunctionGoto = new Goto(instr.get(instr.size() - 1).getLine(), 0);
  285. loopFunctionGoto.setJump(forLoopFunctionStart);
  286. instr.add(loopFunctionGoto);
  287. int forEnd = instr.size() - 1;
  288. f.setJump(forEnd);
  289. setBreakContinueJumps(forLoopFunctionStart, forEnd);
  290. }
  291. private void setBreakContinueJumps(int start, int end)
  292. {
  293. while(!continueStack.empty())
  294. {
  295. continueStack.pop().setJump(start);
  296. }
  297. while(!breakStack.empty())
  298. {
  299. breakStack.pop().setJump(end);
  300. }
  301. }
  302. private void handleUserFunction()
  303. {
  304. consume(LITERAL);
  305. Token t = previous();
  306. consume(OPEN_BRACKET);
  307. ArrayList<String> list = new ArrayList<>();
  308. if(!match(CLOSE_BRACKET))
  309. {
  310. while(true)
  311. {
  312. consume(LITERAL);
  313. list.add(previous().getData().toString());
  314. if(match(CLOSE_BRACKET))
  315. {
  316. break;
  317. }
  318. consume(COMMA);
  319. }
  320. }
  321. String name = t.getData().toString().toLowerCase();
  322. UserFunction uf = new UserFunction(t.getLine(), name, list.toArray(new String[list.size()]));
  323. functions.put(name, instr.size());
  324. instr.add(uf);
  325. consume(OPEN_CURVED_BRACKET);
  326. inFunction = name;
  327. while(!match(CLOSE_CURVED_BRACKET))
  328. {
  329. line();
  330. }
  331. inFunction = null;
  332. instr.add(new Return(instr.get(instr.size() - 1).getLine(), 0));
  333. uf.setJump(instr.size() - 1);
  334. }
  335. private void handleReturn()
  336. {
  337. Token t = previous();
  338. int args = 0;
  339. if(!match(SEMICOLON))
  340. {
  341. args = 1;
  342. expression();
  343. consume(SEMICOLON);
  344. }
  345. instr.add(new Return(t.getLine(), args));
  346. }
  347. private void handleWhile()
  348. {
  349. int whileStart = instr.size() - 1;
  350. Token t = previous();
  351. consume(OPEN_BRACKET);
  352. expression();
  353. While w = new While(t.getLine());
  354. instr.add(w);
  355. consume(CLOSE_BRACKET);
  356. consume(OPEN_CURVED_BRACKET);
  357. while(!match(CLOSE_CURVED_BRACKET))
  358. {
  359. line();
  360. }
  361. addGoto(instr.get(instr.size() - 1).getLine(), whileStart);
  362. int whileEnd = instr.size() - 1;
  363. w.setJump(whileEnd);
  364. setBreakContinueJumps(whileStart, whileEnd);
  365. }
  366. private void handleTry()
  367. {
  368. Try t = new Try(previous().getLine());
  369. instr.add(t);
  370. consume(OPEN_CURVED_BRACKET);
  371. while(!match(CLOSE_CURVED_BRACKET))
  372. {
  373. line();
  374. }
  375. consume(CATCH);
  376. Catch c = new Catch(previous().getLine());
  377. instr.add(c);
  378. t.setJump(instr.size() - 1);
  379. consume(OPEN_CURVED_BRACKET);
  380. while(!match(CLOSE_CURVED_BRACKET))
  381. {
  382. line();
  383. }
  384. c.setJump(instr.size() - 1);
  385. }
  386. private void expression()
  387. {
  388. assignment();
  389. }
  390. private void assignment()
  391. {
  392. logicalOr();
  393. if(match(SET, ADD_SET, SUB_SET, MUL_SET, DIV_SET, MOD_SET, LEFT_SHIFT_SET,
  394. RIGHT_SHIFT_SET, BIT_AND_SET, BIT_XOR_SET, BIT_OR_SET))
  395. {
  396. Token t = previous();
  397. assignment();
  398. addFunction(t.getLine(), 2, t.getType().getName());
  399. lineExpression = true;
  400. }
  401. }
  402. private void logicalOr()
  403. {
  404. logicalAnd();
  405. while(match(OR))
  406. {
  407. Token t = previous();
  408. IfGoto ifGoto = new IfGoto(t.getLine(), true);
  409. instr.add(ifGoto);
  410. logicalAnd();
  411. ifGoto.setJump(instr.size());
  412. addFunction(t.getLine(), 2, t.getType().getName());
  413. }
  414. }
  415. private void logicalAnd()
  416. {
  417. equality();
  418. while(match(AND))
  419. {
  420. Token t = previous();
  421. IfGoto ifGoto = new IfGoto(t.getLine(), false);
  422. instr.add(ifGoto);
  423. equality();
  424. ifGoto.setJump(instr.size());
  425. addFunction(t.getLine(), 2, t.getType().getName());
  426. }
  427. }
  428. private void equality()
  429. {
  430. comparison();
  431. while(match(EQUAL, NOT_EQUAL))
  432. {
  433. Token t = previous();
  434. comparison();
  435. addFunction(t.getLine(), 2, t.getType().getName());
  436. }
  437. }
  438. private void comparison()
  439. {
  440. addition();
  441. while(match(GREATER, GREATER_EQUAL, LESS, LESS_EQUAL))
  442. {
  443. Token t = previous();
  444. addition();
  445. addFunction(t.getLine(), 2, t.getType().getName());
  446. }
  447. }
  448. private void addition()
  449. {
  450. multiplication();
  451. while(match(SUB, ADD))
  452. {
  453. Token t = previous();
  454. multiplication();
  455. addFunction(t.getLine(), 2, t.getType().getName());
  456. }
  457. }
  458. private void multiplication()
  459. {
  460. unary();
  461. while(match(DIV, MUL, MOD))
  462. {
  463. Token t = previous();
  464. unary();
  465. addFunction(t.getLine(), 2, t.getType().getName());
  466. }
  467. }
  468. private void unary()
  469. {
  470. if(match(INVERT, BIT_INVERT, SUB, INC, DEC))
  471. {
  472. Token t = previous();
  473. unary();
  474. addFunction(t.getLine(), 1, t.getType().getName());
  475. if(t.getType() == INC || t.getType() == DEC)
  476. {
  477. lineExpression = true;
  478. }
  479. return;
  480. }
  481. postUnary();
  482. }
  483. private void postUnary()
  484. {
  485. primary();
  486. while(match(INC, DEC))
  487. {
  488. Token t = previous();
  489. addFunction(t.getLine(), 1, "p" + t.getType().getName());
  490. lineExpression = true;
  491. }
  492. }
  493. private void primary()
  494. {
  495. Token t = advance();
  496. switch(t.getType())
  497. {
  498. case FALSE: addConstant(t.getLine(), ConstantBoolean.FALSE); return;
  499. case TRUE: addConstant(t.getLine(), ConstantBoolean.TRUE); return;
  500. case NULL: addConstant(t.getLine(), ConstantNull.NULL); return;
  501. case STRING: addConstant(t.getLine(), new ConstantString(t.getData().toString())); return;
  502. case LABEL: addConstant(t.getLine(), new ConstantString(t.getData().toString().substring(1))); return;
  503. case NUMBER: addConstant(t.getLine(), new ConstantDouble((Double) t.getData())); return;
  504. case OPEN_BRACKET:
  505. expression();
  506. consume(CLOSE_BRACKET);
  507. return;
  508. case LITERAL:
  509. if(match(OPEN_SQUARE_BRACKET))
  510. {
  511. handleArray(t);
  512. }
  513. else if(match(OPEN_BRACKET))
  514. {
  515. handleFunction(t);
  516. }
  517. else
  518. {
  519. addConstant(t.getLine(), getVariable(t.getData().toString()));
  520. }
  521. return;
  522. }
  523. throw new PreScriptException(String.format("unexpected token %s", t.getType()), t.getLine());
  524. }
  525. public void handleFunction(Token t)
  526. {
  527. int args = 0;
  528. if(peek().getType() != CLOSE_BRACKET)
  529. {
  530. while(true)
  531. {
  532. args++;
  533. expression();
  534. if(match(CLOSE_BRACKET))
  535. {
  536. break;
  537. }
  538. consume(COMMA);
  539. }
  540. }
  541. else
  542. {
  543. consume(CLOSE_BRACKET);
  544. }
  545. addFunction(t.getLine(), args, t.getData().toString());
  546. lineExpression = true;
  547. }
  548. public void handleArray(Token t)
  549. {
  550. if(peek().getType() == CLOSE_SQUARE_BRACKET)
  551. {
  552. throw new PreScriptException("empty array access", peek().getLine());
  553. }
  554. int args = 0;
  555. while(true)
  556. {
  557. args++;
  558. expression();
  559. if(match(CLOSE_SQUARE_BRACKET))
  560. {
  561. break;
  562. }
  563. consume(COMMA);
  564. }
  565. instr.add(new Array(t.getLine(), args, getVariable(t.getData().toString())));
  566. }
  567. private Variable getVariable(String name)
  568. {
  569. boolean global = name.startsWith("$");
  570. if(inFunction != null && !global)
  571. {
  572. LocalVariable v = localVars.get(name);
  573. if(v != null)
  574. {
  575. return v;
  576. }
  577. v = new LocalVariable(name);
  578. localVars.put(name, v);
  579. return v;
  580. }
  581. if(global)
  582. {
  583. name = name.substring(1);
  584. }
  585. Variable v = vars.get(name);
  586. if(v != null)
  587. {
  588. return v;
  589. }
  590. v = new Variable(name);
  591. vars.put(name, v);
  592. return v;
  593. }
  594. }