Skip to content

Commit

Permalink
rename some file and remove console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
zuluoaaa committed Mar 31, 2020
1 parent e19b545 commit bfd33c1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
1 change: 0 additions & 1 deletion src/core/ASTnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class ASTNode{
}

initUnaryNode(op,left,value){
console.log({op,left,value})
this.left = left;
return this.initLeafNode(op,value);
}
Expand Down
2 changes: 0 additions & 2 deletions src/core/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const infixParserMap = {
function getPrecedence(){
let {token} = gData;
let infix = infixParserMap[token.type];
console.log(token)
return infix.precedence;
}

Expand All @@ -55,7 +54,6 @@ function parseExpression(precedenceValue) {
){
return left;
}
console.log(left,"?")
let value = getPrecedence();
while (value>precedenceValue){
let type = token.type;
Expand Down
48 changes: 22 additions & 26 deletions src/core/genAST.js → src/core/interpreter.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
const {ASTNode} = require("./ASTnode");
const {gData,tokenTypes,ASTNodeTypes} = require("./token");
const {match,scan,rightPt} = require("./scanner");
const {ASTNodeTypes} = require("./token");
const {errPrint} = require("../init/commons");
const {addVar, assignVal,findVar} = require("./data");
const {assignVal,findVar} = require("./data");
const {Scope} = require("./scope");

function genIfAST(astNode,scope) {
let state = genAST(astNode.left,null,scope);
function interpretIfAST(astNode,scope) {
let state = interpretAST(astNode.left,null,scope);
if(state){
return genAST(astNode.mid,null,scope);
return interpretAST(astNode.mid,null,scope);
}else{
if(astNode.right){
return genAST(astNode.right,null,scope);
return interpretAST(astNode.right,null,scope);
}
}
}

function genWhileAST(astNode,scope) {
function interpretWhileAST(astNode,scope) {
let condition = astNode.left;
let body = astNode.right;
let state = genAST(condition,null,scope);
let state = interpretAST(condition,null,scope);
if(state){
genAST(body,null,scope);
genWhileAST(astNode,scope);
interpretAST(body,null,scope);
interpretWhileAST(astNode,scope);
}
}

function genFunCallAST(astNode,scope) {
function interpretFunCallAST(astNode,scope) {
let childScope = new Scope(scope,"block");
let argument = {};
if(astNode.left
&& astNode.left.op === ASTNodeTypes.T_FUNARGS
&& astNode.left.value.length>0
){
astNode.left.value.forEach((item,index)=>{
argument[index] = genAST(item);
argument[index] = interpretAST(item);
})
}
childScope.set("arguments",argument,ASTNodeTypes.T_ARGUMENT);

let fnAST = scope.get(astNode.value);
fnAST.option = "run";
return genAST(fnAST,null,childScope);
return interpretAST(fnAST,null,childScope);
}

function genAST(astNode,result=null,scope){
function interpretAST(astNode,result=null,scope){
if(!astNode){
return;
}
Expand All @@ -53,15 +51,14 @@ function genAST(astNode,result=null,scope){
}
switch (astNode.op) {
case ASTNodeTypes.T_IF:
return genIfAST(astNode,scope);
return interpretIfAST(astNode,scope);
case ASTNodeTypes.T_GLUE:
genAST(astNode.left,null,scope);
genAST(astNode.right,null,scope);
interpretAST(astNode.left,null,scope);
interpretAST(astNode.right,null,scope);
return;
case ASTNodeTypes.T_WHILE:
return genWhileAST(astNode,scope);
return interpretWhileAST(astNode,scope);
case ASTNodeTypes.T_FUN:
console.log(astNode.option,"ffffffffccccc")
if(astNode.option === "run"){
astNode.option = "";

Expand All @@ -73,17 +70,17 @@ function genAST(astNode,result=null,scope){
}
case ASTNodeTypes.T_FUNCALL:

return genFunCallAST(astNode,scope);
return interpretFunCallAST(astNode,scope);

}

let leftResult,rightResult;

if(astNode.left){
leftResult = genAST(astNode.left,null,scope);
leftResult = interpretAST(astNode.left,null,scope);
}
if(astNode.right){
rightResult = genAST(astNode.right,leftResult,scope);
rightResult = interpretAST(astNode.right,leftResult,scope);
}


Expand Down Expand Up @@ -138,12 +135,11 @@ function genAST(astNode,result=null,scope){
return leftResult !== rightResult;

default:
console.log(astNode);
errPrint(`unknown ASTNode op : ${astNode.op}`);
}
}


module.exports = {
genAST
interpretAST
}
3 changes: 2 additions & 1 deletion src/core/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ function match(type,text){
scan();
return true
}else{
errPrint(`Exception : ${gData.token.type}(${gData.token.value}) !== ${type}(${text})`);
console.log(`Exception : ${gData.token.type}(${gData.token.value}) !== ${type}(${text})`);
errPrint(`Uncaught SyntaxError: Invalid or unexpected token`);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {init} = require("./init/init")
const {scan} = require("./core/scanner");
const {statement} = require("./core/parse");
const {genAST} = require("./core/genAST");
const {interpretAST} = require("./core/interpreter");
const {gData,ASTNodeTypes} = require("./core/token");
function main(){

Expand All @@ -11,12 +11,12 @@ function main(){
scan();
let astNodeTree = statement();
console.log(JSON.stringify(astNodeTree),"astNodeTree");
genAST(astNodeTree,null,gData.gScope);
interpretAST(astNodeTree,null,gData.gScope);

//gData.gScope.set("log",console.log,ASTNodeTypes.T_NATIVE_FUN);

console.log(gData.gScope)

debugger;
console.log("compiled finished");
}

Expand Down

0 comments on commit bfd33c1

Please sign in to comment.