Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
itribs committed Jun 12, 2020
1 parent 5902f7d commit d0e5046
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 55 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
},
"dependencies": {
"element-ui": "^2.13.0",
"lodash": "^4.17.15",
"vue": "^2.6.10"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^4.1.0",
"@vue/cli-service": "^4.1.0",
"vue-template-compiler": "^2.6.10"
}
}
}
2 changes: 1 addition & 1 deletion public/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
}
],
"pluginSetting": {
"height": 673
"height": 450
}
}
100 changes: 98 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,29 +1,125 @@
<template>
<div id="app">
<el-row :gutter="10"
class="main"
style="margin-left:0; margin-right:0;">
<el-col :span="14"
class="main-col code">
<el-input type="textarea"
ref="input"
resize="none"
v-model="code"
wrap="off"
@input="change">
</el-input>
</el-col>
<el-col :span="10"
class="main-col result">
<el-input type="textarea"
resize="none"
ref="ouput"
readonly="true"
wrap="off"
v-model="result"
v-bind:class="color">
</el-input>
</el-col>
</el-row>
</div>
</template>

<script>
import _ from 'lodash'
export default {
name: 'app',
data () {
return {
code: window.getClipboard(),
result: '',
color: 'success'
}
},
mounted () {
let input = this.$refs.input.$el.querySelector('textarea')
input.addEventListener('scroll', this.onscroll, true)
this.change()
},
methods: {
onscroll () {
let input = this.$refs.input.$el.querySelector('textarea')
let ouput = this.$refs.ouput.$el.querySelector('textarea')
ouput.scrollTop = input.scrollTop
},
change () {
let result = window.evaluateCode(this.code)
if (result.error) {
this.result = result.error.message
this.color = 'error'
} else {
let str = ''
for (let i = 0; i < result.value.length; i++) {
str += result.value[i] + '\n'
}
this.result = str
this.color = 'success'
this.onscroll()
}
}
},
mounted () {
created () {
this.change = _.debounce(this.change, 500)
}
}
</script>

<style>
body,
html {
height: 100%;
margin: 0;
padding: 0;
}
.el-textarea,
.el-textarea .el-textarea__inner {
height: 100%;
border: 0;
}
.el-textarea .el-textarea__inner {
padding: 20px;
}
#app {
height: 100%;
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
color: #333;
}
.main {
border: 1px solid #eee;
}
.main textarea {
font-size: 28px;
overflow-x: auto;
font-weight: bold;
}
.main,
.main-col {
height: 100%;
}
.result {
border-left: 1px solid #eee;
}
.result textarea {
overflow: hidden;
}
.main .error textarea {
color: #ff5252;
font-size: 12px;
}
.main .success textarea {
color: #0fb11c;
}
</style>
130 changes: 96 additions & 34 deletions src/interpreter/grammar.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
let Node = require('./node')
let Token = require('./token')
const Node = require('./node')
const Token = require('./token')

/*
Expression := Assignment | Additive
Assignment := Identifier '=' Additive
Additive := Multiplicative | Additive '+' Multiplicative | Additive '-' Multiplicative
Multiplicative := Primary | Primary '*' Multiplicative | Primary '/' Multiplicative | Primary '%' Multiplicative
Primary := Identifier | IntLiteral | FloatLiteral | (Additive)
Additive := Multiplicative | Additive '+' Multiplicative | Additive '-' Multiplicative
Multiplicative := Function | Multiplicative '*' Function | Multiplicative '/' Function | Multiplicative '%' Function
Function := Identifier '(' (Additive (',' Additive)?)? ')' | Primary
Primary := Identifier | IntLiteral | FloatLiteral | '(' Additive ')'
*/

function primary (tokens) {
Expand All @@ -32,31 +33,77 @@ function primary (tokens) {
if (token && token.type == Token.type.RightParen) {
tokens.read()
} else {
throw "缺少右括号"
if (!token) {
tokens.unread()
token = tokens.peek()
}
let e = new Error(`error:格式错误\n缺少右括号\n行:${token.lineNumber}\n列:${token.startColumn}`)
throw e
}
} else {
throw "括号内缺少表达式"
let e = new Error(`error:格式错误\n括号内缺少表达式\n行:${token.lineNumber}\n列:${token.startColumn}`)
throw e
}
break
case Token.type.Unknown:
let e = new Error(`error:格式错误\n发现未知字符\n行:${token.lineNumber}\n列:${token.startColumn}`)
throw e
break
}
}
return node
}

function multiplicative (tokens) {
let child1 = primary(tokens)
let node = child1
function func (tokens) {
let node = null
let token = tokens.peek()
if (child1 && token) {
if (token.type == Token.type.Star || token.type == Token.type.Slash || token.type == Token.type.Percent) {
token = tokens.read()
let child2 = multiplicative(tokens)
if (token && token.type == Token.type.Identifier) {
tokens.read()
let child1 = new Node(Node.type.Identifier, token.text)
token = tokens.peek()
if (token && token.type == Token.type.LeftParen) {
tokens.read()
node = new Node(Node.type.Function, 'Function')
node.addChild(child1)
let child2 = additive(tokens)
if (!token || token.type != Token.type.RightParen) {
if (!token) {
tokens.unread()
token = tokens.peek()
}
let e = new Error(`error:格式错误\n缺少右括号\n行:${token.lineNumber}\n列:${token.startColumn}`)
throw e
}
if (child2) {
node = new Node(Node.type.Multiplicative, token.text)
node.addChild(child1)
node.addChild(child2)
}
} else {
tokens.unread()
}
}
return node
}

function multiplicative (tokens) {
let child1 = primary(tokens)
let node = child1
if (child1) {
while (true) {
let token = tokens.peek()
if (token && (token.type == Token.type.Star || token.type == Token.type.Slash || token.type == Token.type.Percent)) {
token = tokens.read()
let child2 = primary(tokens)
if (child2) {
node = new Node(Node.type.Multiplicative, token.text)
node.addChild(child1)
node.addChild(child2)
child1 = node
} else {
let e = new Error(`error:格式错误\n表达式缺少右边值\n行:${token.lineNumber}\n列:${token.startColumn}`)
throw e
}
} else {
throw "表达式缺少右边值"
break
}
}
}
Expand All @@ -72,10 +119,15 @@ function additive (tokens) {
if (token && (token.type == Token.type.Plus || token.type == Token.type.Minus)) {
token = tokens.read()
let child2 = multiplicative(tokens)
node = new Node(Node.type.Additive, token.text)
node.addChild(child1)
node.addChild(child2)
child1 = node
if (child2) {
node = new Node(Node.type.Additive, token.text)
node.addChild(child1)
node.addChild(child2)
child1 = node
} else {
let e = new Error(`error:格式错误\n表达式缺少右边值\n行:${token.lineNumber}\n列:${token.startColumn}`)
throw e
}
} else {
break
}
Expand Down Expand Up @@ -104,30 +156,39 @@ function assignment (tokens) {
return null
}

function blank (tokens) {
let token = tokens.peek()
if (token && token.type == Token.type.LineBreak) {
tokens.read()
return new Node(Node.type.Blank, '\n')
}
return null
}

function expression (tokens) {
let pos = tokens.getPos()
let node = assignment(tokens)
let node = blank(tokens)
if (!node) {
node = assignment(tokens)
}
if (!node) {
tokens.setPos(pos)
node = additive(tokens)
}
token = tokens.peek()
if (!token || token.type == Token.type.LineBreak) {
if (token) {
tokens.read()
}
} else {
throw "每个语句独占一行"
token = tokens.peek()
}

while (true) {
if (node && node.type != Node.type.Blank) {
token = tokens.peek()
if (token && token.type == Token.type.LineBreak) {
tokens.read()
if (!token || token.type == Token.type.LineBreak) {
if (token) {
tokens.read()
}
} else {
break
let e = new Error(`error:格式错误\n每个语句独占一行\n行:${token.lineNumber}\n列:${token.startColumn}`)
throw e
}
}

return node
}

Expand All @@ -141,6 +202,7 @@ function treeRootNode (tokens) {
break
}
}
tokens.setPos(0)
return node
}

Expand Down
Loading

0 comments on commit d0e5046

Please sign in to comment.