Skip to content

Commit

Permalink
feat: Add Comment && Number
Browse files Browse the repository at this point in the history
  • Loading branch information
liulinboyi committed Feb 24, 2021
1 parent b5e0cb0 commit fa4c9d4
Show file tree
Hide file tree
Showing 18 changed files with 524 additions and 174 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/dist/*.js.map
/examples/*/*.js.map
/.cache/
/src/*/*.js.map
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"type": "node",
"request": "launch",
"args": [
"${workspaceRoot}/src/parser.ts" // 入口文件
"${workspaceRoot}\\examples\\pineapple\\main.ts" // 入口文件
],
"runtimeArgs": [
"--nolazy",
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules\\typescript\\lib"
}
10 changes: 10 additions & 0 deletions examples/pineapple/hello-world-1.pineapple
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$a = 0
print($a)

$b = "这是pineapple"
print($b)

# 这是注释 hahahahahahahhaha \r\n

$c = 1
print($c)
35 changes: 23 additions & 12 deletions examples/pineapple/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 27 additions & 12 deletions examples/pineapple/main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { Execute } from '../../src/backend'
import fs from "fs";
const args = process.argv.slice(2)
if (args[0]) {
let code = ''
// const args = process.argv.slice(2)
// if (args[0]) {
// let code = ''

try {
code = fs.readFileSync(args[0], { encoding: 'utf-8' })
} catch (error) {
console.log(`Error reading file: ${args[0]}`)
}
// try {
// code = fs.readFileSync(args[0], { encoding: 'utf-8' })
// } catch (error) {
// console.log(`Error reading file: ${args[0]}`)
// }

if (code.length > 0) {
Execute(code)
}
}
// if (code.length > 0) {
// Execute(code)
// }
// }


import path from 'path'
let code = ''

try {
code = fs.readFileSync(path.resolve(__dirname,'./hello-world1.pineapple'), { encoding: 'utf-8' })
console.log(code, 'code')
} catch (error) {
console.log(`${error}`)
}

if (code.length > 0) {
Execute(code)
}
27 changes: 23 additions & 4 deletions src/backend.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions src/backend.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Assignment, parse, Print } from "./parser"
import { parse } from "./parser"
import { GlobalVariables } from './definition'
import { Assignment } from "./parser/Assignment"
import { Print } from "./parser/Print"
import { Comment } from "./parser/Comment"

let GlobalVariables: GlobalVariables = {
Variables: {}
Expand All @@ -21,6 +24,9 @@ export function Execute(code: string) {
// parse
ast = parse(code)

console.log(JSON.stringify(ast, null, 4), '\r\rAST')
console.log("--------------------------------------------")

// resolve
resolveAST(g, ast)
}
Expand All @@ -42,6 +48,8 @@ function resolveStatement(g: any, statement: any) {
} else if (statement instanceof Print) {
let print = statement
return resolvePrint(g, print)
} else if (statement instanceof Comment) {
// 注释,什么也不做
} else {
throw new Error("resolveStatement(): undefined statement type.")
}
Expand All @@ -53,21 +61,31 @@ function resolveAssignment(g: any, assignment: any) {
if (varName == "") {
throw new Error("resolveAssignment(): variable name can NOT be empty.")
}
g.Variables[varName] = assignment.String
if (assignment.String !== null && assignment.String !== undefined) {
g.Variables[varName] = assignment.String
} else if (assignment.Number !== null && assignment.Number !== undefined) {
g.Variables[varName] = assignment.Number
} else {
throw new Error("Ivalie value.");
}

return null
}


function resolvePrint(g: any, print: any) {
let varName = ""
varName = print.Variable.Name;
// console.log(varName, 'varName')
// console.log(g, 'g')
if (varName == "") {
throw new Error("resolvePrint(): variable name can NOT be empty.")
}
let str = ""
let ok = false
str = g.Variables[varName]
ok = str ? true : false
// console.log(str, 'str')
ok = str !== null && str !== undefined ? true : false
if (!ok) {
throw new Error(`resolvePrint(): variable '$${varName}'not found.`)
}
Expand Down
50 changes: 46 additions & 4 deletions src/lexer1.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fa4c9d4

Please sign in to comment.