Code.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. package me.hammerle.code;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.Stack;
  6. import me.hammerle.exceptions.PreScriptException;
  7. import me.hammerle.exceptions.NoSuchMethodException;
  8. import me.hammerle.math.Fraction;
  9. public class Code
  10. {
  11. protected final int realLine;
  12. protected final String function;
  13. protected final int pars;
  14. protected Object value;
  15. private Code(String function, int pars, Object value, int realLine)
  16. {
  17. this.function = function;
  18. this.pars = pars;
  19. this.value = value;
  20. this.realLine = realLine;
  21. }
  22. public Code(String function, int pars, int realLine)
  23. {
  24. this(function, pars, null, realLine);
  25. }
  26. public Code(Object value, int realLine)
  27. {
  28. this(null, 0, value, realLine);
  29. }
  30. public void executeFunction(SnuviParser parser, Script sc, Stack<Object> stack)
  31. {
  32. if(function == null)
  33. {
  34. if(value != null)
  35. {
  36. if(value.getClass() == Variable.class)
  37. {
  38. stack.push(sc.getVar(((Variable) value).getName()));
  39. return;
  40. }
  41. stack.push(value);
  42. return;
  43. }
  44. stack.push(value);
  45. return;
  46. }
  47. Object[] input;
  48. if(pars > 0)
  49. {
  50. input = new Object[pars];
  51. for(int i = 0; i < pars; i++)
  52. {
  53. input[i] = stack.pop();
  54. }
  55. }
  56. else
  57. {
  58. input = new Object[0];
  59. }
  60. Object output = parser.parseFunction(sc, function, input);
  61. if(output != Void.TYPE)
  62. {
  63. stack.push(output);
  64. }
  65. }
  66. @Override
  67. public String toString()
  68. {
  69. StringBuilder sb = new StringBuilder("(");
  70. sb.append(realLine);
  71. sb.append(") ");
  72. if(function != null)
  73. {
  74. sb.append("function '");
  75. sb.append(function);
  76. sb.append("' ");
  77. for(int i = 0; i < pars; i++)
  78. {
  79. sb.append("X,");
  80. }
  81. if(pars > 0)
  82. {
  83. sb.deleteCharAt(sb.length() - 1);
  84. }
  85. if(value != null)
  86. {
  87. sb.append("(");
  88. sb.append(value);
  89. sb.append(")");
  90. }
  91. }
  92. else
  93. {
  94. sb.append("push '");
  95. sb.append(value);
  96. sb.append("' ");
  97. if(value != null)
  98. {
  99. sb.append(value.getClass().getSimpleName());
  100. }
  101. return sb.toString();
  102. }
  103. return sb.toString();
  104. }
  105. // -----------------------------------------------------------------------------------
  106. // code builder
  107. // -----------------------------------------------------------------------------------
  108. private static String scriptName = null;
  109. private static int findFirstNonLetterOrDigit(StringBuilder code, int pos)
  110. {
  111. int length = code.length();
  112. while(pos < length)
  113. {
  114. if(!Character.isLetterOrDigit(code.charAt(pos)))
  115. {
  116. return pos;
  117. }
  118. pos++;
  119. }
  120. return -1;
  121. }
  122. private static int findStartOfSyntax(StringBuilder code, int pos)
  123. {
  124. int bracketCounter = 0;
  125. while(pos >= 0 && code.charAt(pos) == ' ')
  126. {
  127. pos--;
  128. }
  129. while(pos >= 0)
  130. {
  131. switch(code.charAt(pos))
  132. {
  133. case ';':
  134. case '{':
  135. case '}':
  136. if(bracketCounter != 0)
  137. {
  138. throw new PreScriptException(scriptName, countChar('\n', pos, code), "unbalanced ()");
  139. }
  140. return pos + 1;
  141. case ' ':
  142. case '+':
  143. case '-':
  144. case '*':
  145. case '/':
  146. case '^':
  147. case '@':
  148. case '=':
  149. case '>':
  150. case '<':
  151. case '!':
  152. case '%':
  153. case ',':
  154. case '&':
  155. case '|':
  156. if(bracketCounter != 0)
  157. {
  158. break;
  159. }
  160. return pos + 1;
  161. case ')':
  162. bracketCounter++;
  163. break;
  164. case '(':
  165. if(bracketCounter == 0)
  166. {
  167. return pos + 1;
  168. }
  169. bracketCounter--;
  170. break;
  171. }
  172. pos--;
  173. }
  174. return 0;
  175. }
  176. private static int findEndOfSyntax(StringBuilder code, int pos)
  177. {
  178. return findEndOfSyntax(code, pos, false);
  179. }
  180. private static int findEndOfSyntax(StringBuilder code, int pos, boolean b)
  181. {
  182. int bracketCounter = 0;
  183. char c;
  184. while(pos < code.length() && code.charAt(pos) == ' ')
  185. {
  186. pos++;
  187. }
  188. while(pos < code.length())
  189. {
  190. c = code.charAt(pos);
  191. if(b && c == '\n')
  192. {
  193. if(bracketCounter != 0)
  194. {
  195. pos++;
  196. continue;
  197. }
  198. return pos;
  199. }
  200. switch(c)
  201. {
  202. case ';':
  203. case '{':
  204. case '}':
  205. if(bracketCounter != 0)
  206. {
  207. throw new PreScriptException(scriptName, countChar('\n', pos, code), "unbalanced ()");
  208. }
  209. return pos;
  210. case ' ':
  211. case '+':
  212. case '-':
  213. case '*':
  214. case '/':
  215. case '^':
  216. case '@':
  217. case '=':
  218. case '>':
  219. case '<':
  220. case '!':
  221. case '%':
  222. case ',':
  223. case '&':
  224. case '|':
  225. if(bracketCounter != 0)
  226. {
  227. break;
  228. }
  229. return pos;
  230. case '(':
  231. bracketCounter++;
  232. break;
  233. case ')':
  234. if(bracketCounter == 0)
  235. {
  236. return pos;
  237. }
  238. bracketCounter--;
  239. break;
  240. }
  241. pos++;
  242. }
  243. return code.length();
  244. }
  245. private static int findChar(char c, StringBuilder code, int pos)
  246. {
  247. int length = code.length();
  248. while(pos < length)
  249. {
  250. if(code.charAt(pos) == c)
  251. {
  252. return pos;
  253. }
  254. pos++;
  255. }
  256. return -1;
  257. }
  258. private static int findSyntax(String find, StringBuilder code, int pos)
  259. {
  260. int length = code.length();
  261. String s = code.toString();
  262. while(pos < length)
  263. {
  264. // additionel check for e.g. difference between + and +=
  265. if(s.startsWith(find, pos) && !s.startsWith(find + "=", pos) && (pos == 0 || code.charAt(pos - 1) != '='))
  266. {
  267. return pos;
  268. }
  269. pos++;
  270. }
  271. return -1;
  272. }
  273. private static int findSyntaxIgnoreString(String find, StringBuilder code, int pos)
  274. {
  275. int length = code.length();
  276. int add = find.length();
  277. char c;
  278. boolean text = false;
  279. while(pos < length)
  280. {
  281. c = code.charAt(pos);
  282. if(text && c != '"')
  283. {
  284. pos++;
  285. continue;
  286. }
  287. else if(c == '"')
  288. {
  289. text = !text;
  290. pos++;
  291. continue;
  292. }
  293. else if(code.substring(pos, Math.min(pos + add, length)).equals(find))
  294. {
  295. return pos;
  296. }
  297. pos++;
  298. }
  299. return -1;
  300. }
  301. private static void changeBiSyntax(String syntax, String f, StringBuilder sb, boolean b)
  302. {
  303. int pos = -1;
  304. int end;
  305. int start;
  306. int slength = syntax.length();
  307. StringBuilder newSyntax;
  308. while(pos < sb.length())
  309. {
  310. pos = findSyntax(syntax, sb, pos + 1);
  311. if(pos == -1)
  312. {
  313. break;
  314. }
  315. start = findStartOfSyntax(sb, pos - 1);
  316. end = findEndOfSyntax(sb, pos + slength);
  317. newSyntax = new StringBuilder(f);
  318. newSyntax.append("(");
  319. if(b)
  320. {
  321. newSyntax.append("\"");
  322. }
  323. newSyntax.append(sb.substring(start, pos).trim());
  324. if(b)
  325. {
  326. newSyntax.append("\"");
  327. }
  328. if(end > pos)
  329. {
  330. newSyntax.append(",");
  331. newSyntax.append(sb.substring(pos + slength, end).trim());
  332. }
  333. newSyntax.append(")");
  334. pos -= end - start - newSyntax.length();
  335. //System.out.println(sb.substring(start, end) + " ===> " + newSyntax);
  336. sb.replace(start, end, newSyntax.toString());
  337. }
  338. }
  339. private static void changeUnSyntax(String syntax, String f, StringBuilder sb)
  340. {
  341. int pos = -1;
  342. int end;
  343. int start;
  344. int slength = syntax.length();
  345. String first;
  346. StringBuilder newSyntax;
  347. while(pos < sb.length())
  348. {
  349. pos = findSyntax(syntax, sb, pos + 1);
  350. if(pos == -1)
  351. {
  352. break;
  353. }
  354. start = findStartOfSyntax(sb, pos - 1);
  355. end = findEndOfSyntax(sb, pos + slength);
  356. newSyntax = new StringBuilder("setvar(\"");
  357. first = sb.substring(start, pos).trim();
  358. newSyntax.append(first);
  359. newSyntax.append("\",");
  360. newSyntax.append(f);
  361. newSyntax.append("(");
  362. newSyntax.append(first);
  363. newSyntax.append(",");
  364. newSyntax.append(sb.substring(pos + slength, end).trim());
  365. newSyntax.append("))");
  366. pos -= end - start - newSyntax.length();
  367. sb.replace(start, end, newSyntax.toString());
  368. }
  369. }
  370. public static int countChar(char find, int end, StringBuilder code)
  371. {
  372. int counter = 0;
  373. int pos = 0;
  374. end = Math.min(end, code.length());
  375. while(pos < end)
  376. {
  377. if(code.charAt(pos) == find)
  378. {
  379. counter++;
  380. }
  381. pos++;
  382. }
  383. return counter;
  384. }
  385. private static void addKeyWordBrackets(String keyWord, StringBuilder sb)
  386. {
  387. String with = keyWord + "()";
  388. int length = keyWord.length();
  389. int pos = 0;
  390. while(true)
  391. {
  392. pos = sb.indexOf(keyWord, pos);
  393. if(pos + length >= sb.length())
  394. {
  395. throw new PreScriptException(scriptName, countChar('\n', pos, sb), "unexpected keyword");
  396. }
  397. else if(pos == -1 ||
  398. (pos >= 1 && Character.isLetterOrDigit(sb.charAt(pos - 1))) ||
  399. (Character.isLetterOrDigit(sb.charAt(pos + length))))
  400. {
  401. break;
  402. }
  403. else if(sb.charAt(pos + length) != '(')
  404. {
  405. sb.replace(pos, pos + length, with);
  406. }
  407. pos += length;
  408. }
  409. }
  410. private static String removeNonSyntax(String s)
  411. {
  412. StringBuilder sb = new StringBuilder(s);
  413. int pos = 0;
  414. while(pos < sb.length())
  415. {
  416. switch(sb.charAt(pos))
  417. {
  418. case ' ':
  419. case '\t':
  420. case '\n':
  421. sb.deleteCharAt(pos);
  422. continue;
  423. }
  424. pos++;
  425. }
  426. return sb.toString();
  427. }
  428. public static Code[] generate(SnuviParser parser, String scriptName, String code, HashMap<String, Integer> gotos)
  429. {
  430. System.out.println("START GENERATE");
  431. long startTime = System.currentTimeMillis();
  432. Code.scriptName = scriptName;
  433. int old = 0;
  434. int pos;
  435. StringBuilder sb = new StringBuilder(code);
  436. // ---------------------------------------------------------------------
  437. // comments
  438. // ---------------------------------------------------------------------
  439. while(true)
  440. {
  441. old = findSyntaxIgnoreString("/*", sb, old);
  442. if(old == -1)
  443. {
  444. break;
  445. }
  446. pos = findSyntaxIgnoreString("*/", sb, old);
  447. if(pos == -1)
  448. {
  449. throw new PreScriptException(scriptName, countChar('\n', old, sb), "/* without */");
  450. }
  451. sb.delete(old, pos + 2);
  452. }
  453. old = 0;
  454. while(true)
  455. {
  456. old = findSyntaxIgnoreString("//", sb, old);
  457. if(old == -1)
  458. {
  459. break;
  460. }
  461. pos = findSyntaxIgnoreString("\n", sb, old);
  462. if(pos == -1)
  463. {
  464. sb.delete(old, sb.length());
  465. break;
  466. }
  467. sb.delete(old, pos + 1);
  468. }
  469. // ---------------------------------------------------------------------
  470. // replacing Strings with #... to get rid of "
  471. // ---------------------------------------------------------------------
  472. pos = 0;
  473. String replacement;
  474. String text;
  475. int stringIndex = 0;
  476. HashMap<String, String> strings = new HashMap<>();
  477. while(pos < sb.length())
  478. {
  479. if(sb.charAt(pos) == '"')
  480. {
  481. old = pos;
  482. pos++;
  483. while(sb.charAt(pos) != '"')
  484. {
  485. pos++;
  486. if(pos >= sb.length())
  487. {
  488. throw new PreScriptException(scriptName, countChar('\n', old, sb), "\" without another");
  489. }
  490. }
  491. pos++;
  492. text = sb.substring(old, pos);
  493. replacement = "#" + stringIndex;
  494. sb.replace(old, pos, replacement);
  495. strings.put(replacement, text);
  496. stringIndex++;
  497. pos -= (pos - old) - replacement.length();
  498. }
  499. pos++;
  500. }
  501. // ---------------------------------------------------------------------
  502. // allowing labels without ;
  503. // ---------------------------------------------------------------------
  504. old = 0;
  505. while(true)
  506. {
  507. pos = findChar('@', sb, old);
  508. if(pos == -1)
  509. {
  510. break;
  511. }
  512. pos++;
  513. old = pos;
  514. pos = findFirstNonLetterOrDigit(sb, pos);
  515. if(pos == -1)
  516. {
  517. break;
  518. }
  519. if(sb.charAt(pos) != ';')
  520. {
  521. sb.insert(pos, ';');
  522. }
  523. }
  524. // ---------------------------------------------------------------------
  525. // allowing key words without ()
  526. // ---------------------------------------------------------------------
  527. addKeyWordBrackets("else", sb);
  528. addKeyWordBrackets("try", sb);
  529. addKeyWordBrackets("catch", sb);
  530. // ---------------------------------------------------------------------
  531. // replacing of function syntax
  532. // ---------------------------------------------------------------------
  533. changeBiSyntax("^", "math.pow", sb, false);
  534. changeBiSyntax("/", "div", sb, false);
  535. changeBiSyntax("%", "math.mod", sb, false);
  536. changeBiSyntax("*", "mul", sb, false);
  537. changeBiSyntax("-", "sub", sb, false);
  538. changeBiSyntax("+", "add", sb, false);
  539. changeBiSyntax("==", "equal", sb, false);
  540. changeBiSyntax("!=", "notequal", sb, false);
  541. changeBiSyntax("<", "less", sb, false);
  542. changeBiSyntax(">", "greater", sb, false);
  543. changeBiSyntax(">=", "greaterequal", sb, false);
  544. changeBiSyntax("<=", "lessequal", sb, false);
  545. changeBiSyntax("||", "or", sb, false);
  546. changeBiSyntax("&&", "and", sb, false);
  547. changeUnSyntax("+=", "add", sb);
  548. changeUnSyntax("-=", "sub", sb);
  549. changeUnSyntax("/=", "div", sb);
  550. changeUnSyntax("*=", "mul", sb);
  551. changeBiSyntax("=", "setvar", sb, true);
  552. // ---------------------------------------------------------------------
  553. // numbers like -5 turn to sub(,5) --> fixing
  554. // ---------------------------------------------------------------------
  555. pos = 0;
  556. while(true)
  557. {
  558. pos = findSyntax("sub(,", sb, pos);
  559. if(pos == -1)
  560. {
  561. break;
  562. }
  563. sb.insert(pos + 4, '0');
  564. pos++;
  565. }
  566. code = sb.toString();
  567. // ---------------------------------------------------------------------
  568. // split code into lines
  569. // ---------------------------------------------------------------------
  570. ArrayList<String> lines = new ArrayList<>();
  571. int lineCounter = 1;
  572. ArrayList<Integer> realCodeLine = new ArrayList<>();
  573. old = 0;
  574. pos = 0;
  575. int length = code.length();
  576. int counter = 0;
  577. int bracketCounter = 0;
  578. boolean closed = true;
  579. Stack<Integer> brackets = new Stack<>();
  580. while(pos < length)
  581. {
  582. switch(code.charAt(pos))
  583. {
  584. case '(':
  585. closed = false;
  586. bracketCounter++;
  587. break;
  588. case ')':
  589. bracketCounter--;
  590. break;
  591. case ';':
  592. if(bracketCounter != 0)
  593. {
  594. throw new PreScriptException(scriptName, lineCounter, "unbalanced ()");
  595. }
  596. lines.add(removeNonSyntax(code.substring(old, pos)));
  597. realCodeLine.add(lineCounter);
  598. old = pos + 1;
  599. closed = true;
  600. break;
  601. case '{':
  602. if(bracketCounter != 0)
  603. {
  604. throw new PreScriptException(scriptName, lineCounter, "unbalanced ()");
  605. }
  606. brackets.push(lineCounter);
  607. counter++;
  608. lines.add(removeNonSyntax(code.substring(old, pos)));
  609. realCodeLine.add(lineCounter);
  610. old = pos + 1;
  611. lines.add("{");
  612. realCodeLine.add(lineCounter);
  613. break;
  614. case '}':
  615. if(!closed)
  616. {
  617. throw new PreScriptException(scriptName, lineCounter, "missing ;");
  618. }
  619. else if(bracketCounter != 0)
  620. {
  621. throw new PreScriptException(scriptName, lineCounter, "unbalanced ()");
  622. }
  623. counter--;
  624. if(counter < 0)
  625. {
  626. throw new PreScriptException(scriptName, lineCounter, "unexpected }");
  627. }
  628. old = pos + 1;
  629. lines.add("}");
  630. realCodeLine.add(lineCounter);
  631. brackets.pop();
  632. break;
  633. case '\n':
  634. lineCounter++;
  635. break;
  636. }
  637. pos++;
  638. }
  639. // well the second check should be useless
  640. if(counter != 0 && !brackets.isEmpty())
  641. {
  642. throw new PreScriptException(scriptName, brackets.pop(), "unbalanced {}");
  643. }
  644. // ---------------------------------------------------------------------
  645. // generating byte code
  646. // ---------------------------------------------------------------------
  647. ArrayList<Code> co = new ArrayList<>();
  648. Stack<Integer> parCounter = new Stack<>();
  649. char current;
  650. char previous;
  651. int layer = 0;
  652. int line;
  653. boolean noFunction;
  654. LinkedList<Integer> whileStart = new LinkedList<>();
  655. String s;
  656. for(int i = 0; i < lines.size(); i++)
  657. {
  658. s = lines.get(i);
  659. if(s.isEmpty())
  660. {
  661. continue;
  662. }
  663. else if(s.startsWith("while"))
  664. {
  665. whileStart.push(co.size());
  666. }
  667. else if(s.charAt(0) == '@')
  668. {
  669. gotos.put(s.substring(1), co.size());
  670. continue;
  671. }
  672. line = realCodeLine.get(i);
  673. previous = ',';
  674. noFunction = true;
  675. pos = s.length() - 1;
  676. while(pos >= 0)
  677. {
  678. current = s.charAt(pos);
  679. switch(current)
  680. {
  681. case ')':
  682. parCounter.add(0);
  683. if(previous != ',' && previous != ')')
  684. {
  685. throw new PreScriptException(scriptName, line, "unexpected )");
  686. }
  687. old = pos;
  688. break;
  689. case '(':
  690. if(previous != ')')
  691. {
  692. parCounter.set(parCounter.size() - 1, parCounter.lastElement() + 1);
  693. if(noFunction)
  694. {
  695. co.add(new Code(Code.convertInput(strings, s.substring(pos + 1, old), true), line));
  696. }
  697. }
  698. old = pos;
  699. pos--;
  700. while(pos >= 0 && (Character.isLetterOrDigit(s.charAt(pos))
  701. || s.charAt(pos) == '_' || s.charAt(pos) == '.'))
  702. {
  703. pos--;
  704. }
  705. pos++;
  706. String functionName = s.substring(pos, old).toLowerCase();
  707. if(!parser.isRegisteredFunction(functionName))
  708. {
  709. throw new NoSuchMethodException(scriptName, line, functionName);
  710. }
  711. co.add(new Code(functionName, parCounter.pop(), line));
  712. noFunction = false;
  713. break;
  714. case ',':
  715. parCounter.set(parCounter.size() - 1, parCounter.lastElement() + 1);
  716. if(noFunction)
  717. {
  718. co.add(new Code(Code.convertInput(strings, s.substring(pos + 1, old), true), line));
  719. }
  720. old = pos;
  721. noFunction = true;
  722. break;
  723. case '{':
  724. layer++;
  725. co.add(new Code("goto", 0, layer, line));
  726. break;
  727. case '}':
  728. co.add(new Code("goto", 0, layer, line));
  729. layer--;
  730. break;
  731. }
  732. pos--;
  733. previous = current;
  734. }
  735. }
  736. // ---------------------------------------------------------------------
  737. // generating gotos of key words
  738. // ---------------------------------------------------------------------
  739. Code[] c = co.toArray(new Code[co.size()]);
  740. for(int i = 0; i < c.length; i++)
  741. {
  742. if("goto".equals(c[i].function) && c[i].value != null)
  743. {
  744. Object value = c[i].value;
  745. if((int) value <= 0)
  746. {
  747. continue;
  748. }
  749. for(int j = i + 1; j < c.length; j++)
  750. {
  751. if(value.equals(c[j].value))
  752. {
  753. c[i].value = j - i;
  754. switch(c[i-1].function)
  755. {
  756. case "while":
  757. c[j].value = whileStart.pollLast() - j - 1;
  758. break;
  759. /*case "if":
  760. case "else":
  761. case "try":
  762. case "catch":
  763. c[j].value = 0;
  764. break;*/
  765. default:
  766. c[j].value = 0;
  767. break;
  768. }
  769. break;
  770. }
  771. }
  772. }
  773. }
  774. // end
  775. System.out.println("END GENERATE - " + (System.currentTimeMillis() - startTime));
  776. //java.util.Arrays.stream(c).forEach(cod -> System.out.println(cod.toString()));
  777. //gotos.forEach((k, v) -> System.out.println("Lable " + k + " " + v));
  778. //System.exit(0);
  779. return c;
  780. }
  781. public static Object convertInput(HashMap<String, String> map, String s, boolean variable)
  782. {
  783. if(s == null)
  784. {
  785. return null;
  786. }
  787. s = s.trim();
  788. if(s.length() == 0)
  789. {
  790. return "";
  791. }
  792. if(s.charAt(0) == '"' && s.charAt(s.length() - 1) == '"')
  793. {
  794. if(s.length() == 1)
  795. {
  796. System.out.println("Convert-Input - this should never happen");
  797. return "";
  798. }
  799. return s.substring(1, s.length() - 1);
  800. }
  801. else if(variable && s.startsWith("#"))
  802. {
  803. return convertInput(null, map.get(s), false);
  804. }
  805. else if(s.equals("true"))
  806. {
  807. return true;
  808. }
  809. else if(s.equals("false"))
  810. {
  811. return false;
  812. }
  813. else if(s.equals("null"))
  814. {
  815. return null;
  816. }
  817. try
  818. {
  819. if(s.contains("/"))
  820. {
  821. String[] parts = s.split("/");
  822. return new Fraction(Long.parseLong(parts[0].trim()), Long.parseLong(parts[1].trim()));
  823. }
  824. return new Fraction(Long.parseLong(s));
  825. }
  826. catch(NumberFormatException ex)
  827. {
  828. try
  829. {
  830. return Fraction.fromDouble(Double.parseDouble(s));
  831. }
  832. catch(NumberFormatException ex2)
  833. {
  834. if(variable)
  835. {
  836. return new Variable(s);
  837. }
  838. return s;
  839. }
  840. }
  841. }
  842. public static Object convertInput(String s)
  843. {
  844. return convertInput(null, s, false);
  845. }
  846. }