From ca9d20b75bbb77062cc9c5980d6aab7d08d2c1a0 Mon Sep 17 00:00:00 2001 From: Matej Genci Date: Thu, 12 Mar 2020 20:14:27 +0000 Subject: [PATCH] Mock responses from server --- src/App.js | 28 ++-- src/Comm.js | 448 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 465 insertions(+), 11 deletions(-) create mode 100644 src/Comm.js diff --git a/src/App.js b/src/App.js index 9b85cbf..fc43946 100644 --- a/src/App.js +++ b/src/App.js @@ -7,6 +7,8 @@ import ButtonStrip from './view/ButonStrip' import CodeEditor from './view/CodeEditor' import UserIO from './view/UserIO' +import {sendWaccCode, astMetaToGraphData} from './Comm' + import './App.css'; class App extends React.Component { @@ -25,7 +27,8 @@ class App extends React.Component { arm: { code: "", markers: [], - } + }, + graphData: [{}], } } @@ -38,11 +41,22 @@ class App extends React.Component { }) } + processWaccCode = (code) => { + let rsp = sendWaccCode(code) + let graph = astMetaToGraphData(rsp.astMeta) + + this.setState({ + arm: {code: rsp.armCode}, + js: {code: rsp.jsCode}, + graphData: graph + }) + } + render() { return (
{ alert("Compiling wacc\n\"" + this.state.wacc.code + "\"") }} + onCompileClick={(e) => { this.processWaccCode(this.state.wacc.code) }} onStepJsClick={(e) => { this.setState({js: {code: "Hello World!"}}) }} />
@@ -63,7 +77,7 @@ class App extends React.Component {
@@ -73,12 +87,4 @@ class App extends React.Component { } } -const testGraphData = [{ - name: "WACC Program", children: [{ - name: "BinOP", attributes: { operation: "Add" }, - children: [{ name: "Const", attributes: { value: "1" } }, { name: "VarIdent", attributes: { ident: "x" } }] - }] -}]; - - export default App; diff --git a/src/Comm.js b/src/Comm.js new file mode 100644 index 0000000..6a62b62 --- /dev/null +++ b/src/Comm.js @@ -0,0 +1,448 @@ +import React from 'react' +import axios from 'axios' + +const SERVER_ADDR = "http://localhost:3000/" +const SERVER_ENDPOINT = "compile/wacc" + +const testGraphData = [{ + name: "WACC Program", children: [{ + name: "BinOP", attributes: { operation: "Add" }, + children: [{ name: "Const", attributes: { value: "1" } }, { name: "VarIdent", attributes: { ident: "x" } }] + }] + }]; + + +export function astMetaToGraphData(astMeta) { + return testGraphData +} + +export function sendWaccCode(code) { + let ADDR = SERVER_ADDR + SERVER_ENDPOINT + console.log(`Sending ${code} to ${ADDR}`) + axios.get(ADDR) + .then(res => { + console.log(res) + }) + .catch(reason => { + console.log(reason) + return + }) + + return { + "armCode": ".data\n\nmsg_0:\n\t\t.word 50\n\t\t.ascii \"NullReferenceError: dereference a null reference\\n\\0\"\nmsg_1:\n\t\t.word 5\n\t\t.ascii \"%.*s\\0\"\nmsg_2:\n\t\t.word 3\n\t\t.ascii \"%d\\0\"\n\n\t\t.text\n\t\t\n\t\t.global main\n\t\tmain:\n\t\tPUSH {lr}\n\t\tSUB sp, sp, #12\n\t\tLDR r0, =8\n\t\tBL malloc\n\t\tMOV r4, r0\n\t\tLDR r5, =2\n\t\tLDR r0, =4\n\t\tBL malloc\n\t\tSTR r0, [r4]\n\t\tSTR r5, [r0]\n\t\tLDR r5, =3\n\t\tLDR r0, =4\n\t\tBL malloc\n\t\tSTR r0, [r4, #4]\n\t\tSTR r5, [r0]\n\t\tSTR r4, [sp, #8]\n\t\tLDR r0, =8\n\t\tBL malloc\n\t\tMOV r4, r0\n\t\tLDR r5, =1\n\t\tLDR r0, =4\n\t\tBL malloc\n\t\tSTR r0, [r4]\n\t\tSTR r5, [r0]\n\t\tLDR r5, [sp, #8]\n\t\tLDR r0, =4\n\t\tBL malloc\n\t\tSTR r0, [r4, #4]\n\t\tSTR r5, [r0]\n\t\tSTR r4, [sp, #4]\n\t\tLDR r0, =16\n\t\tBL malloc\n\t\tMOV r4, r0\n\t\tLDR r5, =3\n\t\tSTR r5, [r0]\n\t\tLDR r5, =1\n\t\tSTR r5, [r4, #4]\n\t\tLDR r5, =2\n\t\tSTR r5, [r4, #8]\n\t\tLDR r5, =3\n\t\tSTR r5, [r4, #12]\n\t\tSTR r4, [sp]\n\t\tLDR r4, [sp, #4]\n\t\tMOV r0, r4\n\t\tBL p_check_null_pointer\n\t\tLDR r4, [r4]\n\t\tMOV r0, r4\n\t\tBL p_read_int\n\t\tADD sp, sp, #12\n\t\tLDR r0, =0\n\t\tPOP {pc}\n\t\t.ltorg\n\t\tp_check_null_pointer:\n\t\tPUSH {lr}\n\t\tCMP r0, #0\n\t\tLDREQ r0, =msg_0\n\t\tBLEQ p_throw_runtime_error\n\t\tPOP {pc}\n\t\tp_throw_runtime_error:\n\t\tBL p_print_string\n\t\tMOV r0, #-1\n\t\tBL exit\n\t\tp_print_string:\n\t\tPUSH {lr}\n\t\tLDR r1, [r0]\n\t\tADD r2, r0, #4\n\t\tLDR r0, =msg_1\n\t\tADD r0, r0, #4\n\t\tBL printf\n\t\tMOV r0, #0\n\t\tBL fflush\n\t\tPOP {pc}\n\t\tp_read_int:\n\t\tPUSH {lr}\n\t\tMOV r1, r0\n\t\tLDR r0, =msg_2\n\t\tADD r0, r0, #4\n\t\tBL scanf\n\t\tPOP {pc}\n", + "jsCode": "var p = { \"fst\": 2, \"snd\": 3 };\nvar q = { \"fst\": 1, \"snd\": p };\nvar a = [1, 2, 3, \b\b];\nq[\"fst\"] = window.prompt(\"\");\n", + "astMeta": { + "nodes": { + "3": { + "name": "Variable: p", + "children": [], + "waccStart": { + "lineNum": 2, + "charNum": 17 + }, + "waccEnd": { + "lineNum": 2, + "charNum": 18 + }, + "armLineNums": [] + }, + "4": { + "name": "INT constant: 2", + "children": [], + "waccStart": { + "lineNum": 2, + "charNum": 29 + }, + "waccEnd": { + "lineNum": 2, + "charNum": 30 + }, + "armLineNums": [ + 22 + ] + }, + "5": { + "name": "INT constant: 3", + "children": [], + "waccStart": { + "lineNum": 2, + "charNum": 32 + }, + "waccEnd": { + "lineNum": 2, + "charNum": 33 + }, + "armLineNums": [ + 27 + ] + }, + "6": { + "name": "New pair", + "children": [ + 4, + 5 + ], + "waccStart": { + "lineNum": 2, + "charNum": 21 + }, + "waccEnd": { + "lineNum": 2, + "charNum": 34 + }, + "armLineNums": [ + 19, + 20, + 21, + 23, + 24, + 25, + 26, + 28, + 29, + 30, + 31 + ] + }, + "7": { + "name": "Declare pair(INT, INT)", + "children": [ + 3, + 6 + ], + "waccStart": { + "lineNum": 2, + "charNum": 2 + }, + "waccEnd": { + "lineNum": 2, + "charNum": 34 + }, + "armLineNums": [ + 32 + ], + "jsStart": { + "lineNum": 1, + "charNum": 1 + }, + "jsEnd": { + "lineNum": 1, + "charNum": 33 + } + }, + "9": { + "name": "Variable: q", + "children": [], + "waccStart": { + "lineNum": 3, + "charNum": 18 + }, + "waccEnd": { + "lineNum": 3, + "charNum": 19 + }, + "armLineNums": [] + }, + "10": { + "name": "INT constant: 1", + "children": [], + "waccStart": { + "lineNum": 3, + "charNum": 30 + }, + "waccEnd": { + "lineNum": 3, + "charNum": 31 + }, + "armLineNums": [ + 36 + ] + }, + "11": { + "name": "Variable: p", + "children": [], + "waccStart": { + "lineNum": 3, + "charNum": 33 + }, + "waccEnd": { + "lineNum": 3, + "charNum": 34 + }, + "armLineNums": [ + 41 + ] + }, + "12": { + "name": "New pair", + "children": [ + 10, + 11 + ], + "waccStart": { + "lineNum": 3, + "charNum": 22 + }, + "waccEnd": { + "lineNum": 3, + "charNum": 35 + }, + "armLineNums": [ + 33, + 34, + 35, + 37, + 38, + 39, + 40, + 42, + 43, + 44, + 45 + ] + }, + "13": { + "name": "Declare pair(INT, pair)", + "children": [ + 9, + 12 + ], + "waccStart": { + "lineNum": 3, + "charNum": 2 + }, + "waccEnd": { + "lineNum": 3, + "charNum": 35 + }, + "armLineNums": [ + 46 + ], + "jsStart": { + "lineNum": 2, + "charNum": 1 + }, + "jsEnd": { + "lineNum": 2, + "charNum": 33 + } + }, + "15": { + "name": "Variable: a", + "children": [], + "waccStart": { + "lineNum": 4, + "charNum": 8 + }, + "waccEnd": { + "lineNum": 4, + "charNum": 9 + }, + "armLineNums": [] + }, + "16": { + "name": "INT constant: 1", + "children": [], + "waccStart": { + "lineNum": 4, + "charNum": 13 + }, + "waccEnd": { + "lineNum": 4, + "charNum": 14 + }, + "armLineNums": [ + 52 + ] + }, + "17": { + "name": "INT constant: 2", + "children": [], + "waccStart": { + "lineNum": 4, + "charNum": 16 + }, + "waccEnd": { + "lineNum": 4, + "charNum": 17 + }, + "armLineNums": [ + 54 + ] + }, + "18": { + "name": "INT constant: 3", + "children": [], + "waccStart": { + "lineNum": 4, + "charNum": 19 + }, + "waccEnd": { + "lineNum": 4, + "charNum": 20 + }, + "armLineNums": [ + 56 + ] + }, + "19": { + "name": "Array", + "children": [ + 16, + 17, + 18 + ], + "waccStart": { + "lineNum": 4, + "charNum": 12 + }, + "waccEnd": { + "lineNum": 4, + "charNum": 21 + }, + "armLineNums": [ + 47, + 48, + 49, + 50, + 51, + 53, + 55, + 57 + ] + }, + "20": { + "name": "Declare INT[0]", + "children": [ + 15, + 19 + ], + "waccStart": { + "lineNum": 4, + "charNum": 2 + }, + "waccEnd": { + "lineNum": 4, + "charNum": 21 + }, + "armLineNums": [ + 58 + ], + "jsStart": { + "lineNum": 3, + "charNum": 1 + }, + "jsEnd": { + "lineNum": 3, + "charNum": 24 + } + }, + "22": { + "name": "Variable: q", + "children": [], + "waccStart": { + "lineNum": 5, + "charNum": 11 + }, + "waccEnd": { + "lineNum": 5, + "charNum": 12 + }, + "armLineNums": [ + 59 + ] + }, + "23": { + "name": "First pair elem", + "children": [ + 22 + ], + "waccStart": { + "lineNum": 5, + "charNum": 7 + }, + "waccEnd": { + "lineNum": 5, + "charNum": 12 + }, + "armLineNums": [ + 60, + 61, + 62 + ] + }, + "24": { + "name": "Read INT", + "children": [ + 23 + ], + "waccStart": { + "lineNum": 5, + "charNum": 2 + }, + "waccEnd": { + "lineNum": 5, + "charNum": 12 + }, + "armLineNums": [ + 63, + 64 + ], + "jsStart": { + "lineNum": 4, + "charNum": 1 + }, + "jsEnd": { + "lineNum": 4, + "charNum": 31 + } + }, + "0": { + "name": "Block", + "children": [ + 7, + 13, + 20, + 24 + ], + "waccStart": { + "lineNum": 2, + "charNum": 2 + }, + "waccEnd": { + "lineNum": 5, + "charNum": 12 + }, + "armLineNums": [] + }, + "26": { + "name": "Function: #main", + "children": [ + 0 + ], + "waccStart": { + "lineNum": 2, + "charNum": 2 + }, + "waccEnd": { + "lineNum": 5, + "charNum": 12 + }, + "armLineNums": [ + 17, + 66, + 67 + ], + "jsStart": { + "lineNum": 1, + "charNum": 1 + }, + "jsEnd": { + "lineNum": 5, + "charNum": 1 + } + } + } + } + } +} \ No newline at end of file -- GitLab