Compiler.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. package me.hammerle.snuviscript.compiler;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import me.hammerle.snuviscript.code.FunctionLoader;
  5. import me.hammerle.snuviscript.code.ISnuviLogger;
  6. import me.hammerle.snuviscript.code.InputProvider;
  7. import me.hammerle.snuviscript.constants.ConstantBoolean;
  8. import me.hammerle.snuviscript.constants.ConstantDouble;
  9. import me.hammerle.snuviscript.constants.ConstantNull;
  10. import me.hammerle.snuviscript.constants.ConstantString;
  11. import me.hammerle.snuviscript.exceptions.PreScriptException;
  12. import me.hammerle.snuviscript.token.Token;
  13. import me.hammerle.snuviscript.token.TokenType;
  14. import static me.hammerle.snuviscript.token.TokenType.*;
  15. import me.hammerle.snuviscript.variable.Variable;
  16. public class Compiler
  17. {
  18. private final ISnuviLogger logger;
  19. private int index = 0;
  20. private Token[] tokens = null;
  21. private final ArrayList<Instruction> instr = new ArrayList<>();
  22. private final HashMap<String, Integer> labels = new HashMap<>();
  23. private final HashMap<String, Variable> vars = new HashMap<>();
  24. public Compiler(ISnuviLogger logger)
  25. {
  26. this.logger = logger;
  27. }
  28. private void addConstant(int line, InputProvider ip)
  29. {
  30. instr.add(new Constant(line, ip));
  31. }
  32. private void addFunction(int line, int args, String name)
  33. {
  34. instr.add(new Function(line, args, FunctionLoader.getFunction(name)));
  35. }
  36. private boolean match(TokenType... types)
  37. {
  38. for(TokenType type : types)
  39. {
  40. if(check(type))
  41. {
  42. advance();
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. private boolean check(TokenType type)
  49. {
  50. if(isAtEnd())
  51. {
  52. return false;
  53. }
  54. return peek().getType() == type;
  55. }
  56. private Token advance()
  57. {
  58. if(!isAtEnd())
  59. {
  60. index++;
  61. }
  62. return previous();
  63. }
  64. private boolean isAtEnd()
  65. {
  66. return peek().getType() == EOF;
  67. }
  68. private Token peek()
  69. {
  70. return tokens[index];
  71. }
  72. private Token previous()
  73. {
  74. return tokens[index - 1];
  75. }
  76. private Token consume(TokenType type)
  77. {
  78. if(check(type))
  79. {
  80. return advance();
  81. }
  82. throw new PreScriptException(String.format("exptected %s got %s", type, peek().getType()), peek().getLine());
  83. }
  84. public Script2 compile(Token[] tokens)
  85. {
  86. this.tokens = tokens;
  87. index = 0;
  88. instr.clear();
  89. labels.clear();
  90. vars.clear();
  91. while(!isAtEnd())
  92. {
  93. line();
  94. }
  95. for(Instruction i : instr)
  96. {
  97. logger.print(i.toString(), null, null, null, null, -1);
  98. }
  99. return null;
  100. }
  101. private void line()
  102. {
  103. int oldIndex = index;
  104. Token t = advance();
  105. switch(t.getType())
  106. {
  107. case IF: handleIf(); break;
  108. case LABEL: labels.put(previous().getData().toString(), instr.size()); break;
  109. case SEMICOLON: break;
  110. case FOR: handleFor(); break;
  111. case BREAK:
  112. instr.add(new Break(previous().getLine()));
  113. consume(SEMICOLON);
  114. break;
  115. case CONTINUE:
  116. instr.add(new Continue(previous().getLine()));
  117. consume(SEMICOLON);
  118. break;
  119. case FUNCTION: handleUserFunction(); break;
  120. case RETURN: handleReturn(); break;
  121. case WHILE: handleWhile(); break;
  122. case TRY: handleTry(); break;
  123. default:
  124. index = oldIndex;
  125. expression();
  126. consume(SEMICOLON);
  127. }
  128. }
  129. private void handleIf()
  130. {
  131. Token t = previous();
  132. consume(OPEN_BRACKET);
  133. expression();
  134. instr.add(new If(t.getLine()));
  135. consume(CLOSE_BRACKET);
  136. consume(OPEN_CURVED_BRACKET);
  137. while(!match(CLOSE_CURVED_BRACKET))
  138. {
  139. line();
  140. }
  141. handleElseIf();
  142. }
  143. private void handleElseIf()
  144. {
  145. while(match(ELSEIF))
  146. {
  147. Token t = previous();
  148. consume(OPEN_BRACKET);
  149. expression();
  150. instr.add(new Else(t.getLine()));
  151. consume(CLOSE_BRACKET);
  152. consume(OPEN_CURVED_BRACKET);
  153. while(!match(CLOSE_CURVED_BRACKET))
  154. {
  155. line();
  156. }
  157. }
  158. handleElse();
  159. }
  160. private void handleElse()
  161. {
  162. if(match(ELSE))
  163. {
  164. instr.add(new Else(previous().getLine()));
  165. consume(OPEN_CURVED_BRACKET);
  166. while(!match(CLOSE_CURVED_BRACKET))
  167. {
  168. line();
  169. }
  170. }
  171. }
  172. private void handleFor()
  173. {
  174. Token t = previous();
  175. consume(OPEN_BRACKET);
  176. if(!match(SEMICOLON))
  177. {
  178. expression();
  179. consume(SEMICOLON);
  180. }
  181. if(!match(SEMICOLON))
  182. {
  183. expression();
  184. consume(SEMICOLON);
  185. }
  186. if(!match(CLOSE_BRACKET))
  187. {
  188. expression();
  189. consume(CLOSE_BRACKET);
  190. }
  191. instr.add(new For(t.getLine()));
  192. consume(OPEN_CURVED_BRACKET);
  193. while(!match(CLOSE_CURVED_BRACKET))
  194. {
  195. line();
  196. }
  197. }
  198. private void handleUserFunction()
  199. {
  200. consume(LITERAL);
  201. Token t = previous();
  202. consume(OPEN_BRACKET);
  203. ArrayList<String> list = new ArrayList<>();
  204. if(!match(CLOSE_BRACKET))
  205. {
  206. while(true)
  207. {
  208. consume(LITERAL);
  209. list.add(previous().getData().toString());
  210. if(match(CLOSE_BRACKET))
  211. {
  212. break;
  213. }
  214. consume(COMMA);
  215. }
  216. }
  217. instr.add(new UserFunction(t.getLine(), t.getData().toString(), list.toArray(new String[list.size()])));
  218. consume(OPEN_CURVED_BRACKET);
  219. while(!match(CLOSE_CURVED_BRACKET))
  220. {
  221. line();
  222. }
  223. }
  224. private void handleReturn()
  225. {
  226. if(!match(SEMICOLON))
  227. {
  228. expression();
  229. consume(SEMICOLON);
  230. }
  231. }
  232. private void handleWhile()
  233. {
  234. Token t = previous();
  235. consume(OPEN_BRACKET);
  236. expression();
  237. instr.add(new While(t.getLine()));
  238. consume(CLOSE_BRACKET);
  239. consume(OPEN_CURVED_BRACKET);
  240. while(!match(CLOSE_CURVED_BRACKET))
  241. {
  242. line();
  243. }
  244. }
  245. private void handleTry()
  246. {
  247. instr.add(new Try(previous().getLine()));
  248. consume(OPEN_CURVED_BRACKET);
  249. while(!match(CLOSE_CURVED_BRACKET))
  250. {
  251. line();
  252. }
  253. consume(CATCH);
  254. instr.add(new Catch(previous().getLine()));
  255. consume(OPEN_CURVED_BRACKET);
  256. while(!match(CLOSE_CURVED_BRACKET))
  257. {
  258. line();
  259. }
  260. }
  261. private void expression()
  262. {
  263. assignment();
  264. }
  265. private void assignment()
  266. {
  267. logicalOr();
  268. if(match(SET, ADD_SET, SUB_SET, MUL_SET, DIV_SET, MOD_SET, LEFT_SHIFT_SET,
  269. RIGHT_SHIFT_SET, BIT_AND_SET, BIT_XOR_SET, BIT_OR_SET))
  270. {
  271. Token t = previous();
  272. assignment();
  273. addFunction(t.getLine(), 2, t.getType().getName());
  274. }
  275. }
  276. private void logicalOr()
  277. {
  278. logicalAnd();
  279. while(match(OR))
  280. {
  281. Token t = previous();
  282. logicalAnd();
  283. addFunction(t.getLine(), 2, t.getType().getName());
  284. }
  285. }
  286. private void logicalAnd()
  287. {
  288. equality();
  289. while(match(AND))
  290. {
  291. Token t = previous();
  292. equality();
  293. addFunction(t.getLine(), 2, t.getType().getName());
  294. }
  295. }
  296. private void equality()
  297. {
  298. comparison();
  299. while(match(EQUAL, NOT_EQUAL))
  300. {
  301. Token t = previous();
  302. comparison();
  303. addFunction(t.getLine(), 2, t.getType().getName());
  304. }
  305. }
  306. private void comparison()
  307. {
  308. addition();
  309. while(match(GREATER, GREATER_EQUAL, LESS, LESS_EQUAL))
  310. {
  311. Token t = previous();
  312. addition();
  313. addFunction(t.getLine(), 2, t.getType().getName());
  314. }
  315. }
  316. private void addition()
  317. {
  318. multiplication();
  319. while(match(SUB, ADD))
  320. {
  321. Token t = previous();
  322. multiplication();
  323. addFunction(t.getLine(), 2, t.getType().getName());
  324. }
  325. }
  326. private void multiplication()
  327. {
  328. unary();
  329. while(match(DIV, MUL))
  330. {
  331. Token t = previous();
  332. unary();
  333. addFunction(t.getLine(), 2, t.getType().getName());
  334. }
  335. }
  336. private void unary()
  337. {
  338. if(match(INVERT, BIT_INVERT, SUB, INC, DEC))
  339. {
  340. Token t = previous();
  341. unary();
  342. addFunction(t.getLine(), 1, t.getType().getName());
  343. return;
  344. }
  345. postUnary();
  346. }
  347. private void postUnary()
  348. {
  349. primary();
  350. while(match(INC, DEC))
  351. {
  352. Token t = previous();
  353. addFunction(t.getLine(), 1, "p" + t.getType().getName());
  354. }
  355. }
  356. private void primary()
  357. {
  358. Token t = advance();
  359. switch(t.getType())
  360. {
  361. case FALSE: addConstant(t.getLine(), ConstantBoolean.FALSE); return;
  362. case TRUE: addConstant(t.getLine(), ConstantBoolean.FALSE); return;
  363. case NULL: addConstant(t.getLine(), ConstantNull.NULL); return;
  364. case STRING: addConstant(t.getLine(), new ConstantString(t.getData().toString())); return;
  365. case LABEL: addConstant(t.getLine(), new ConstantString(t.getData().toString().substring(1))); return;
  366. case NUMBER: addConstant(t.getLine(), new ConstantDouble((Double) t.getData())); return;
  367. case OPEN_BRACKET:
  368. expression();
  369. consume(CLOSE_BRACKET);
  370. return;
  371. case LITERAL:
  372. if(match(OPEN_SQUARE_BRACKET))
  373. {
  374. handleArray(t);
  375. }
  376. else if(match(OPEN_BRACKET))
  377. {
  378. handleFunction(t);
  379. }
  380. else
  381. {
  382. addConstant(t.getLine(), getVariable(t.getData().toString()));
  383. }
  384. return;
  385. }
  386. throw new PreScriptException(String.format("unexpected token %s", t.getType()), t.getLine());
  387. }
  388. public void handleFunction(Token t)
  389. {
  390. int args = 0;
  391. if(peek().getType() != CLOSE_BRACKET)
  392. {
  393. while(true)
  394. {
  395. args++;
  396. expression();
  397. if(match(CLOSE_BRACKET))
  398. {
  399. break;
  400. }
  401. consume(COMMA);
  402. }
  403. }
  404. else
  405. {
  406. consume(CLOSE_BRACKET);
  407. }
  408. addFunction(t.getLine(), args, t.getData().toString());
  409. }
  410. public void handleArray(Token t)
  411. {
  412. if(peek().getType() == CLOSE_SQUARE_BRACKET)
  413. {
  414. throw new PreScriptException("empty array access", peek().getLine());
  415. }
  416. int args = 0;
  417. while(true)
  418. {
  419. args++;
  420. expression();
  421. if(match(CLOSE_SQUARE_BRACKET))
  422. {
  423. break;
  424. }
  425. consume(COMMA);
  426. }
  427. instr.add(new Array(t.getLine(), args, getVariable(t.getData().toString())));
  428. }
  429. private Variable getVariable(String name)
  430. {
  431. Variable v = vars.get(name);
  432. if(v != null)
  433. {
  434. return v;
  435. }
  436. v = new Variable(name);
  437. vars.put(name, v);
  438. return v;
  439. }
  440. }