Browse Source

feat: implement text to visual UI parser for commands

Implement parser for assignment command
Implement parser for functions call (writer,reader)
Lucas de Souza 2 years ago
parent
commit
17cead86e5
1 changed files with 78 additions and 0 deletions
  1. 78 0
      js/util/parseFromVisual.js

+ 78 - 0
js/util/parseFromVisual.js

@@ -1,4 +1,5 @@
 import { IVProgParser } from "../ast/ivprogParser";
+
 import * as Expressions from "../ast/expressions";
 import { Types } from "../typeSystem/types";
 import { convertBoolToString } from "../typeSystem/parsers";
@@ -51,6 +52,82 @@ function getOpType (op) {
       return TYPES.LOGIC;
   }
 }
+/**
+  * @param {Commands.Assign} assingment
+  * */
+function assignmentWalker (assingment) {
+  let variable = null
+  if (assingment instanceof Commands.ArrayIndexAssign) {
+    const line = expressionWalker(assingment.line);
+    let arrayClass = "vector";
+    let column = null;
+    if (assingment.column) {
+      arrayClass = "matrix";
+      column = expressionWalker(assingment.column);
+    }
+    variable = [
+      {
+        instance: "expression",
+        type: TYPES.VARIABLE,
+        class: arrayClass,
+        column: column,
+        line: line,
+        value: assingment.id,
+      },
+    ];
+  } else {
+    variable = [
+      { instance: "expression", type: TYPES.VARIABLE, value: assingment.id },
+    ]
+  }
+  const expression = expressionWalker(assingment.expression)
+  return {
+    type: "attribution",
+    variable,
+    expression
+  }
+}
+
+/**
+ * @param {Command} command
+  * */
+function commandWalker (command) {
+  if (command instanceof Commands.FunctionCall) {
+    return functionCallWalker(command)
+  } else if (command instanceof Commands.Assign) {
+    return assignmentWalker(command)
+  }
+  throw new Error("not implemented")
+}
+
+/**
+ * @param {Commands.FunctionCall} functionCall
+  * */
+function functionCallWalker (functionCall) {
+  let name = functionCall.id;
+  if (name.indexOf(".") !== -1) {
+    name = name.split(".")[1]
+  }
+  if (name === "$read") {
+    const variable = functionCall.actualParameters.map(expressionWalker)[0]
+    return {
+      type:"reader",
+      variable
+    }
+  }
+  if (name === "$write") {
+    const content = functionCall.actualParameters.map(expressionWalker)
+    const lastInput = content[content.length - 1][0];
+    // if lastInput is an object with value === '\n', newLine is true
+    const newLine = lastInput.value && lastInput.value.match(/\n/) !== null
+    return {
+      type:"writer",
+      newLine,
+      content
+    }
+  }
+  throw new Error("not implemented")
+}
 /**
  * @param {Commands.Function} func
  * */
@@ -75,6 +152,7 @@ function functionWalker (func) {
   funcDeclaration.variables_list = func.variablesDeclarations.map(
     variableDeclarationWalker
   );
+  funcDeclaration.commands = func.commands.map(commandWalker)
   return funcDeclaration;
 }