-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7d20eb7
Showing
14 changed files
with
1,126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/node_modules/ | ||
/src/*.js.map | ||
/dist/*.js.map | ||
/.cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
// 使用 IntelliSense 了解相关属性。 | ||
// 悬停以查看现有属性的描述。 | ||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Program", | ||
"program": "${workspaceFolder}/src/backend.js", | ||
"request": "launch", | ||
"skipFiles": [ | ||
"<node_internals>/**" | ||
], | ||
"type": "pwa-node" | ||
}, | ||
{ | ||
"name": "Current TS File", | ||
"type": "node", | ||
"request": "launch", | ||
"args": [ | ||
"${workspaceRoot}/src/parser.ts" // 入口文件 | ||
], | ||
"runtimeArgs": [ | ||
"--nolazy", | ||
"-r", | ||
"ts-node/register" | ||
], | ||
"sourceMaps": true, | ||
"cwd": "${workspaceRoot}", | ||
"protocol": "inspector", | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen" | ||
} | ||
] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# pineapple-ts | ||
|
||
TypeScript implementation of pineapple language (https://github.com/karminski/pineapple) as a personal exercise. | ||
|
||
[karminski/pineapple](https://github.com/karminski/pineapple) | ||
|
||
PINEAPPLE |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"devDependencies": { | ||
"ts-node": "^9.1.1", | ||
"typescript": "^4.1.3" | ||
}, | ||
"scripts": { | ||
"test": "ts-node ./src/backend.ts" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Assignment, parse, Print } from "./parser" | ||
import { GlobalVariables } from './definition' | ||
|
||
let GlobalVariables: GlobalVariables = { | ||
Variables: {} | ||
} | ||
|
||
|
||
function NewGlobalVariables() { | ||
var g = GlobalVariables | ||
g.Variables = {} | ||
return g | ||
} | ||
|
||
|
||
function Execute(code: string) { | ||
var ast = {} | ||
|
||
let g = NewGlobalVariables() | ||
|
||
// parse | ||
ast = parse(code) | ||
|
||
// resolve | ||
resolveAST(g, ast) | ||
} | ||
|
||
function resolveAST(g: any, ast: any) { | ||
if (ast.Statements.length == 0) { | ||
throw new Error("resolveAST(): no code to execute, please check your input.") | ||
} | ||
for (let statement of ast.Statements) { | ||
resolveStatement(g, statement) | ||
} | ||
return null | ||
} | ||
|
||
function resolveStatement(g: any, statement: any) { | ||
if (statement instanceof Assignment) { | ||
let assignment = statement | ||
return resolveAssignment(g, assignment) | ||
} else if (statement instanceof Print) { | ||
let print = statement | ||
return resolvePrint(g, print) | ||
} else { | ||
throw new Error("resolveStatement(): undefined statement type.") | ||
} | ||
} | ||
|
||
function resolveAssignment(g: any, assignment: any) { | ||
let varName = "" | ||
varName = assignment.Variable.Name; | ||
if (varName == "") { | ||
throw new Error("resolveAssignment(): variable name can NOT be empty.") | ||
} | ||
g.Variables[varName] = assignment.String | ||
return null | ||
} | ||
|
||
|
||
function resolvePrint(g: any, print: any) { | ||
let varName = "" | ||
varName = print.Variable.Name; | ||
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 | ||
if (!ok) { | ||
throw new Error(`resolvePrint(): variable '$${varName}'not found.`) | ||
} | ||
console.log(str) | ||
return null | ||
} | ||
|
||
|
||
// test | ||
Execute(`$a = "你好,我是pineapple11" | ||
print($a) | ||
`) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export interface GlobalVariables { | ||
Variables: { | ||
[index: string]: string | ||
} | ||
} | ||
|
||
|
||
export interface Keywords { | ||
print: number, | ||
[index: string]: any | ||
} | ||
|
||
export interface TokenNameMap { | ||
[index: number]: any | ||
} | ||
|
||
export interface Variable { | ||
LineNum?: number, | ||
Name?: string, | ||
} |
Oops, something went wrong.