Browse Source

Implement initial LanguageService prototype

Lucas de Souza 6 năm trước cách đây
mục cha
commit
e315601718
7 tập tin đã thay đổi với 94 bổ sung30 xóa
  1. 28 3
      index.html
  2. 1 1
      js/io/domInput.js
  3. 8 2
      js/io/domOutput.js
  4. 28 23
      js/main.js
  5. 1 1
      js/processor/definedFunctions.js
  6. 19 0
      js/services/languageService.js
  7. 9 0
      tests/test39.spec.js

+ 28 - 3
index.html

@@ -2,6 +2,25 @@
 <html>
 <head>
   <link rel="stylesheet" type="text/css" href="js/semantic/semantic.min.css">
+  <style>
+    .ivprog-io-output {
+      border: 1px solid gainsboro;
+      width: 80%;
+      height: 360px;
+      overflow-y: scroll;
+      background-color: black;
+      color: white;
+    }
+
+    .ivprog-io-input {
+      width: 80%;
+      margin-top: 10px;
+      border: 1.5px solid green;
+      height: 3rem;
+      color: black;
+      font-size: 12pt;
+    }
+  </style>
   <title></title>
 </head>
 <body>
@@ -27,13 +46,19 @@
         </textarea>
       </div>
       <div class="row">
-          <button class="ui button" id="btn">Generate AST</button>
+          <button class="ui button" id="btn">Run</button>
       </div>
     </div>
     <div class="six wide column">
-      <div style="overflow-y: scroll; height: 20%;">
-          <pre id="json-renderer" class="ui right floated"></pre>
+      <div id="dom-out" class="ivprog-io-output">
+
       </div>
+      <input id="dom-in" type="text" class="ivprog-io-input">
+    </div>
+    <div class="six wide column">
+        <div style="overflow-y: scroll; height: 70%;">
+            <pre id="json-renderer" class="ui right floated"></pre>
+        </div>
     </div>
   </div>
   

+ 1 - 1
js/io/domInput.js

@@ -29,7 +29,7 @@ export class DOMInput extends Input{
 
   notifyInput (text) {
     this.listeners.forEach(resolve => {
-      resolve(l);
+      resolve(text);
     })
     this.listeners.splice(0, this.listeners.length);
   }

+ 8 - 2
js/io/domOutput.js

@@ -9,7 +9,13 @@ export class DOMOutput extends Output {
   }
 
   sendOutput (text) {
-    const line = $(`<span class='ivprog-io-output> ${text} </span>`);
-    this.el.append(line);
+    text = text.replace(/"/g,'');
+    text = text.replace("\n", '</br>');
+    const span = $('<span />').addClass('ivprog-io-output-text').html(text);
+    this.el.append(span);
+  }
+
+  clear () {
+    this.el.empty();
   }
 }

+ 28 - 23
js/main.js

@@ -6,6 +6,8 @@ import * as Commands from './ast/commands';
 import { IVProgParser } from './ast/ivprogParser';
 import Lexers from '../grammar/';
 import { IVProgProcessor } from './processor/ivprogProcessor';
+import {DOMInput} from './io/domInput';
+import {DOMOutput} from './io/domOutput';
 
 const lang = 'pt_br';
 
@@ -29,27 +31,30 @@ const input = `programa {
 //     console.log('\n')
 //     i++;
 // }
-const anaSin = new IVProgParser(input, ivprogLexer);
-const proc = new IVProgProcessor(anaSin.parseTree());
-proc.interpretAST().then( sto => {
-  console.log(sto.applyStore('a'));
-}).catch(e => console.log(e));
-// try {
-//   const data = anaSin.parseTree();
-//   console.log(data);
-//   var editor = new JsonEditor('#json-renderer', data);
-//   $('#btn').click( () => {
-//     const input = $('#input').val();
-//     const analiser = new IVProgParser(input, ivprogLexer);
-//     try {
-//       const data = analiser.parseTree();
-//       console.log(data);
-//       editor.load(data);  
-//     } catch (error) {
-//       alert(error);
-//     }
+// const anaSin = new IVProgParser(input, ivprogLexer);
+const editor = new JsonEditor('#json-renderer', {});
+const domIn = new DOMInput('#dom-in');
+const domOut = new DOMOutput('#dom-out');
+// proc.interpretAST().then( sto => {
+//   console.log(sto.applyStore('a'));
+// }).catch(e => console.log(e));
+try {
+  $('#btn').click( () => {
+    const input = $('#input').val();
+    const analiser = new IVProgParser(input, ivprogLexer);
+    try {
+      const data = analiser.parseTree();
+      const proc = new IVProgProcessor(data);
+      proc.registerInput(domIn);
+      domOut.clear();
+      proc.registerOutput(domOut);
+      proc.interpretAST().then(sto => editor.load(sto.store))
+        .catch( e => alert(e));
+    } catch (error) {
+      alert(error);
+    }
     
-//   });
-// } catch(a) {
-//   console.log(a);
-// }
+  });
+} catch(a) {
+  console.log(a);
+}

+ 1 - 1
js/processor/definedFunctions.js

@@ -4,7 +4,7 @@ import {Types} from './../ast/types';
 function createOutputFun () {
   const block = new Commands.CommandBlock([], [new Commands.SysCall('$write')]);
   const func = new Commands.Function('$write', Types.VOID,
-    [new Commands.FormalParameter(Types.ALL, 'p1', 0, true)],
+    [new Commands.FormalParameter(Types.ALL, 'p1', 0, false)],
     block);
   return func;
 }

+ 19 - 0
js/services/languageService.js

@@ -0,0 +1,19 @@
+import Lexers from './../../grammar/';
+export class LanguageService {
+
+  constructor () {
+    throw new Error('LanguageService class must not be instantiated!');
+  }
+
+  static getLang () {
+    const lang = localStorage.getItem('ivprog.lang');
+    if (lang === null) {
+      throw new Error("Internal Error. User language information has not been set");
+    }
+    return lang;
+  }
+
+  static getCurrentLexer () {
+    return Lexers[LanguageService.getLang()];
+  }
+}

+ 9 - 0
tests/test39.spec.js

@@ -0,0 +1,9 @@
+import { LanguageService } from './../js/services/languageService';
+describe('LanguageService.getLang() ', function () {
+  beforeEach(() => {
+    localStorage.setItem('ivprog.lang', 'pt_br');
+  });
+  it("should return the value associated to the key ivprog.lang, inside localStorage", function () {
+    expect(LanguageService.getLang()).toEqual('pt_br');
+  });
+});