main.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import {
  2. InputStream,
  3. CommonTokenStream
  4. } from 'antlr4/index';
  5. import * as Commands from './ast/commands';
  6. import { IVProgParser } from './ast/ivprogParser';
  7. import Lexers from '../grammar/';
  8. const lang = 'pt_br';
  9. const ivprogLexer = Lexers[lang];
  10. const input = `programa {
  11. const real C = 6.8-5.8+1
  12. funcao abc() {
  13. inteiro a = 8
  14. se (a * C > 80) {
  15. a = 0
  16. } senao se(verdadeiro) {
  17. a = -1
  18. fun()
  19. }
  20. }
  21. funcao real fun() {
  22. retorne 3
  23. }
  24. }`;
  25. // const lexer = new ivprogLexer(new InputStream(input));
  26. // const stream = new CommonTokenStream(lexer);
  27. // stream.fill();
  28. // let i = 1;
  29. // let token = null;
  30. // while ((token = stream.LT(i)).type !== ivprogLexer.EOF && token.type !== ivprogLexer.WHITESPACE) {
  31. // console.log(`${token.type}-${token.text}`);
  32. // console.log('\n')
  33. // i++;
  34. // }
  35. const anaSin = new IVProgParser(input, ivprogLexer);
  36. try {
  37. const data = anaSin.parseTree();
  38. console.log(data);
  39. var editor = new JsonEditor('#json-renderer', data);
  40. $('#btn').click( () => {
  41. const input = $('#input').val();
  42. const analiser = new IVProgParser(input, ivprogLexer);
  43. try {
  44. const data = analiser.parseTree();
  45. console.log(data);
  46. editor.load(data);
  47. } catch (error) {
  48. alert(error);
  49. }
  50. });
  51. } catch(a) {
  52. console.log(a);
  53. }