Skip to content

Commit

Permalink
大规模重构:新增进程结构和执行机模块;完善引用机制设计;完善测试
Browse files Browse the repository at this point in the history
  • Loading branch information
bd4sur committed Mar 8, 2019
1 parent e4247d5 commit 335c150
Show file tree
Hide file tree
Showing 10 changed files with 979 additions and 64 deletions.
2 changes: 1 addition & 1 deletion asm/aurora.testcase.factorial.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"qualifiedName":"aurora.testcase.factorial","name":"factorial","AST":{"variables":["fac","n"],"symbols":[],"strings":["\"【SSC编译】普通阶乘 10!=\""],"slists":[{"type":"SLIST","index":0,"children":["$1"],"isQuoted":false,"parameters":[],"body":null},{"type":"LAMBDA","index":1,"parentIndex":0,"children":[],"isQuoted":false,"parameters":[],"body":"$2"},{"type":"SLIST","index":2,"parentIndex":1,"children":["begin","$3","$10","$11","$13"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":3,"parentIndex":2,"children":["define","&0","$4"],"isQuoted":false,"parameters":[],"body":null},{"type":"LAMBDA","index":4,"parentIndex":3,"children":[],"isQuoted":false,"parameters":["&1"],"body":"$5"},{"type":"SLIST","index":5,"parentIndex":4,"children":["if","$6","#1","$7"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":6,"parentIndex":5,"children":["=","&1","#0"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":7,"parentIndex":5,"children":["*","&1","$8"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":8,"parentIndex":7,"children":["&0","$9"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":9,"parentIndex":8,"children":["-","&1","#2"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":10,"parentIndex":2,"children":["display","*0"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":11,"parentIndex":2,"children":["display","$12"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":12,"parentIndex":11,"children":["&0","#3"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":13,"parentIndex":2,"children":["newline"],"isQuoted":false,"parameters":[],"body":null}],"constants":["0","1","1","10"],"refIndexes":{"*":1,"$":14,"!":0,"&":2,"#":4,"^":0}},"ASM":["call @$1","halt",";; [SSC] Function @ $1","@$1",";; [SSC] DEFINE","push @$4","store &0","push *0","display","push #3","call &0","display","newline","return",";; [SSC] Function @ $4","@$4","store &1",";; [SSC] IF","load &1","push #0","=","iftrue @IF-TRUE-TEMP0","load &1","load &1","push #2","-","call &0","*","goto @END-IF-TEMP1","@IF-TRUE-TEMP0","push #1","@END-IF-TEMP1","return"],"labelDict":{"@$1":3,"@$4":15,"@IF-TRUE-TEMP0":29,"@END-IF-TEMP1":31}}
{"qualifiedName":"aurora.testcase.factorial","name":"factorial","AST":{"variables":["fac","n"],"symbols":[],"strings":["\"【SSC编译】普通阶乘 10!=\""],"slists":[{"type":"SLIST","index":0,"children":["$1"],"isQuoted":false,"parameters":[],"body":null},{"type":"LAMBDA","index":1,"parentIndex":0,"children":[],"isQuoted":false,"parameters":[],"body":"$2"},{"type":"SLIST","index":2,"parentIndex":1,"children":["begin","$3","$10","$11","$13"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":3,"parentIndex":2,"children":["define","&0","$4"],"isQuoted":false,"parameters":[],"body":null},{"type":"LAMBDA","index":4,"parentIndex":3,"children":[],"isQuoted":false,"parameters":["&1"],"body":"$5"},{"type":"SLIST","index":5,"parentIndex":4,"children":["if","$6","#1","$7"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":6,"parentIndex":5,"children":["=","&1","#0"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":7,"parentIndex":5,"children":["*","&1","$8"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":8,"parentIndex":7,"children":["&0","$9"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":9,"parentIndex":8,"children":["-","&1","#2"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":10,"parentIndex":2,"children":["display","*0"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":11,"parentIndex":2,"children":["display","$12"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":12,"parentIndex":11,"children":["&0","#3"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":13,"parentIndex":2,"children":["newline"],"isQuoted":false,"parameters":[],"body":null}],"constants":["0","1","1","10"],"refIndexes":{"STRING":1,"SLIST":14,"SYMBOL":0,"VARIABLE":2,"CONSTANT":4,"CLOSURE":0}},"ASM":["call @$1","halt",";; [SSC] Function @ $1","@$1","push $2","return",";; [SSC] Function @ $4","@$4","store &1","push $5","return"],"labelDict":{"@$1":3,"@$4":7}}
2 changes: 1 addition & 1 deletion asm/aurora.testcase.generator.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"qualifiedName":"aurora.testcase.generator","name":"generator","AST":{"variables":["count","generator","g","init","Kont"],"symbols":[],"strings":["\"【SSC编译】生成器:\"","\" \""],"slists":[{"type":"SLIST","index":0,"children":["$1"],"isQuoted":false,"parameters":[],"body":null},{"type":"LAMBDA","index":1,"parentIndex":0,"children":[],"isQuoted":false,"parameters":[],"body":"$2"},{"type":"SLIST","index":2,"parentIndex":1,"children":["begin","$3","$4","$5","$16","$17","$19","$20"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":3,"parentIndex":2,"children":["define","&0","#0"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":4,"parentIndex":2,"children":["define","&1","#f"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":5,"parentIndex":2,"children":["define","&2","$6"],"isQuoted":false,"parameters":[],"body":null},{"type":"LAMBDA","index":6,"parentIndex":5,"children":[],"isQuoted":false,"parameters":[],"body":"$7"},{"type":"SLIST","index":7,"parentIndex":6,"children":["$8","#2"],"isQuoted":false,"parameters":[],"body":null},{"type":"LAMBDA","index":8,"parentIndex":7,"children":[],"isQuoted":false,"parameters":["&3"],"body":"$9"},{"type":"SLIST","index":9,"parentIndex":8,"children":["begin","$10","$13","$15","&3"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":10,"parentIndex":9,"children":["call/cc","$11"],"isQuoted":false,"parameters":[],"body":null},{"type":"LAMBDA","index":11,"parentIndex":10,"children":[],"isQuoted":false,"parameters":["&4"],"body":"$12"},{"type":"SLIST","index":12,"parentIndex":11,"children":["set!","&1","&4"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":13,"parentIndex":9,"children":["set!","&3","$14"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":14,"parentIndex":13,"children":["+","&3","#1"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":15,"parentIndex":9,"children":["set!","&0","&3"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":16,"parentIndex":2,"children":["display","*0"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":17,"parentIndex":2,"children":["display","$18"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":18,"parentIndex":17,"children":["&2"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":19,"parentIndex":2,"children":["display","*1"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":20,"parentIndex":2,"children":["if","$21","$22","$23"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":21,"parentIndex":20,"children":[">=","&0","#3"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":22,"parentIndex":20,"children":["newline"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":23,"parentIndex":20,"children":["display","$24"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":24,"parentIndex":23,"children":["&1","#4"],"isQuoted":false,"parameters":[],"body":null}],"constants":["0","1","0","10","666"],"refIndexes":{"*":2,"$":25,"!":0,"&":5,"#":5,"^":0}},"ASM":["call @$1","halt",";; [SSC] Function @ $1","@$1",";; [SSC] DEFINE","push #0","store &0",";; [SSC] DEFINE","push #f","store &1",";; [SSC] DEFINE","push @$6","store &2","push *0","display","call &2","display","push *1","display",";; [SSC] IF","load &0","push #3",">=","iftrue @IF-TRUE-TEMP0","push #4","call &1","display","goto @END-IF-TEMP1","@IF-TRUE-TEMP0","newline","@END-IF-TEMP1","return",";; [SSC] Function @ $6","@$6","push #2","call @$8","return",";; [SSC] Function @ $8","@$8","store &3",";; [SSC] Current Continuation captured, stored in &CC-$11-TEMP2","capturecc &CC-$11-TEMP2","load &CC-$11-TEMP2","call @$11","@&CC-$11-TEMP2",";; [SSC] SET!","load &3","push #1","+","set! &3",";; [SSC] SET!","load &3","set! &0","load &3","return",";; [SSC] Function @ $11","@$11","store &4",";; [SSC] SET!","load &4","set! &1","return"],"labelDict":{"@$1":3,"@IF-TRUE-TEMP0":28,"@END-IF-TEMP1":30,"@$6":33,"@$8":38,"@&CC-$11-TEMP2":44,"@$11":56}}
{"qualifiedName":"aurora.testcase.generator","name":"generator","AST":{"variables":["count","generator","g","init","Kont"],"symbols":[],"strings":["\"【SSC编译】生成器:\"","\" \""],"slists":[{"type":"SLIST","index":0,"children":["$1"],"isQuoted":false,"parameters":[],"body":null},{"type":"LAMBDA","index":1,"parentIndex":0,"children":[],"isQuoted":false,"parameters":[],"body":"$2"},{"type":"SLIST","index":2,"parentIndex":1,"children":["begin","$3","$4","$5","$16","$17","$19","$20"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":3,"parentIndex":2,"children":["define","&0","#0"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":4,"parentIndex":2,"children":["define","&1","#f"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":5,"parentIndex":2,"children":["define","&2","$6"],"isQuoted":false,"parameters":[],"body":null},{"type":"LAMBDA","index":6,"parentIndex":5,"children":[],"isQuoted":false,"parameters":[],"body":"$7"},{"type":"SLIST","index":7,"parentIndex":6,"children":["$8","#2"],"isQuoted":false,"parameters":[],"body":null},{"type":"LAMBDA","index":8,"parentIndex":7,"children":[],"isQuoted":false,"parameters":["&3"],"body":"$9"},{"type":"SLIST","index":9,"parentIndex":8,"children":["begin","$10","$13","$15","&3"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":10,"parentIndex":9,"children":["call/cc","$11"],"isQuoted":false,"parameters":[],"body":null},{"type":"LAMBDA","index":11,"parentIndex":10,"children":[],"isQuoted":false,"parameters":["&4"],"body":"$12"},{"type":"SLIST","index":12,"parentIndex":11,"children":["set!","&1","&4"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":13,"parentIndex":9,"children":["set!","&3","$14"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":14,"parentIndex":13,"children":["+","&3","#1"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":15,"parentIndex":9,"children":["set!","&0","&3"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":16,"parentIndex":2,"children":["display","*0"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":17,"parentIndex":2,"children":["display","$18"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":18,"parentIndex":17,"children":["&2"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":19,"parentIndex":2,"children":["display","*1"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":20,"parentIndex":2,"children":["if","$21","$22","$23"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":21,"parentIndex":20,"children":[">=","&0","#3"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":22,"parentIndex":20,"children":["newline"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":23,"parentIndex":20,"children":["display","$24"],"isQuoted":false,"parameters":[],"body":null},{"type":"SLIST","index":24,"parentIndex":23,"children":["&1","#4"],"isQuoted":false,"parameters":[],"body":null}],"constants":["0","1","0","10","666"],"refIndexes":{"STRING":2,"SLIST":25,"SYMBOL":0,"VARIABLE":5,"CONSTANT":5}},"ASM":["call @$1","halt",";; [SSC] Function @ $1","@$1",";; [SSC] DEFINE","push #0","store &0",";; [SSC] DEFINE","push #f","store &1",";; [SSC] DEFINE","push @$6","store &2","push *0","display","call &2","display","push *1","display",";; [SSC] IF","load &0","push #3",">=","iftrue @IF-TRUE-TEMP0","push #4","call &1","display","goto @END-IF-TEMP1","@IF-TRUE-TEMP0","newline","@END-IF-TEMP1","return",";; [SSC] Function @ $6","@$6","push #2","call @$8","return",";; [SSC] Function @ $8","@$8","store &3",";; [SSC] Current Continuation captured, stored in &CC-$11-TEMP2","capturecc &CC-$11-TEMP2","load &CC-$11-TEMP2","call @$11","@&CC-$11-TEMP2",";; [SSC] SET!","load &3","push #1","+","set! &3",";; [SSC] SET!","load &3","set! &0","load &3","return",";; [SSC] Function @ $11","@$11","store &4",";; [SSC] SET!","load &4","set! &1","return"],"labelDict":{"@$1":3,"@IF-TRUE-TEMP0":28,"@END-IF-TEMP1":30,"@$6":33,"@$8":38,"@&CC-$11-TEMP2":44,"@$11":56}}
104 changes: 57 additions & 47 deletions source/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,45 +93,48 @@ const OBJECT_TYPE = {
// SLIST的子类型(没有用)
"LAMBDA": "LAMBDA",
"QUOTED_SLIST": "QUOTED_SLIST",

// 标签
"LABEL": "LABEL",
};

// 引用前缀
const REF_PREFIX = {
"REF_STRING": "*",
"REF_SLIST": "$",
"REF_SYMBOL": "!",
"REF_VARIABLE": "&",
"REF_CONSTANT": "#",
"REF_CLOSURE": "^",
"REF_CONTINUATION": "~",
"STRING": "*",
"SLIST": "$",
"SYMBOL": "!",
"VARIABLE": "&",
"CONSTANT": "#",
"CLOSURE": "^",
"CONTINUATION": "~",
};

const getRefIndex = function(ref) {
if(ref === undefined) return undefined;
return parseInt(ref.substring(1));
return ref.substring(1);
};
const getRefType = function(ref) {
if(ref === undefined) return undefined;
if(ref[0] === REF_PREFIX.REF_STRING) {
return OBJECT_TYPE.REF_STRING;
if(ref[0] === REF_PREFIX['STRING']) {
return "REF_STRING";
}
else if(ref[0] === REF_PREFIX.REF_SLIST) {
return OBJECT_TYPE.REF_SLIST;
else if(ref[0] === REF_PREFIX['SLIST']) {
return "REF_SLIST";
}
else if(ref[0] === REF_PREFIX.REF_SYMBOL) {
return OBJECT_TYPE.REF_SYMBOL;
else if(ref[0] === REF_PREFIX['SYMBOL']) {
return "REF_SYMBOL";
}
else if(ref[0] === REF_PREFIX.REF_VARIABLE) {
return OBJECT_TYPE.REF_VARIABLE;
else if(ref[0] === REF_PREFIX['VARIABLE']) {
return "REF_VARIABLE";
}
else if(ref[0] === REF_PREFIX.REF_CONSTANT) {
return OBJECT_TYPE.REF_CONSTANT;
else if(ref[0] === REF_PREFIX['CONSTANT']) {
return "REF_CONSTANT";
}
else if(ref[0] === REF_PREFIX.REF_CLOSURE) {
return OBJECT_TYPE.REF_CLOSURE;
else if(ref[0] === REF_PREFIX['CLOSURE']) {
return "REF_CLOSURE";
}
else if(ref[0] === REF_PREFIX.REF_CONTINUATION) {
return OBJECT_TYPE.REF_CONTINUATION;
else if(ref[0] === REF_PREFIX['CONTINUATION']) {
return "REF_CONTINUATION";
}
else {
return null;
Expand All @@ -141,10 +144,10 @@ const getRefType = function(ref) {
const makeRef = function(type, index) {
if(index === undefined) return undefined;
if(isNaN(index)) {
return `${REF_PREFIX["REF_"+type]}${index}`;
return `${REF_PREFIX[type]}${index}`;
}
else {
return `${REF_PREFIX["REF_"+type]}${parseInt(index)}`;
return `${REF_PREFIX[type]}${parseInt(index)}`;
}
}

Expand All @@ -162,6 +165,9 @@ const TypeOfToken = function(token) {
if(token[0] === '\'') {
return OBJECT_TYPE.SYMBOL;
}
else if(token[0] === '@') {
return OBJECT_TYPE.LABEL;
}
else if(token === '#t' || token === '#f') {
return OBJECT_TYPE.BOOLEAN;
}
Expand All @@ -187,34 +193,32 @@ const AST = function () {
this.constants = new Array();

this.refIndexes = new Object();
this.refIndexes[REF_PREFIX.REF_STRING] = 0;
this.refIndexes[REF_PREFIX.REF_SLIST] = 0;
this.refIndexes[REF_PREFIX.REF_SYMBOL] = 0;
this.refIndexes[REF_PREFIX.REF_VARIABLE] = 0;
this.refIndexes[REF_PREFIX.REF_CONSTANT] = 0;
this.refIndexes[REF_PREFIX.REF_CLOSURE] = 0;
this.refIndexes['STRING'] = 0;
this.refIndexes['SLIST'] = 0;
this.refIndexes['SYMBOL'] = 0;
this.refIndexes['VARIABLE'] = 0;
this.refIndexes['CONSTANT'] = 0;

return this;
}
AST.prototype = {
GetObject: function(ref) {
// TODO 输入检查
let prefix = ref[0];
// let index = parseInt(ref.substring(1));
let index = getRefIndex(ref);
if(prefix === REF_PREFIX.REF_STRING) {
if(prefix === REF_PREFIX['STRING']) {
return this.strings[index];
}
else if(prefix === REF_PREFIX.REF_SLIST) {
else if(prefix === REF_PREFIX['SLIST']) {
return this.slists[index];
}
else if(prefix === REF_PREFIX.REF_SYMBOL) {
else if(prefix === REF_PREFIX['SYMBOL']) {
return this.symbols[index];
}
else if(prefix === REF_PREFIX.REF_VARIABLE) {
else if(prefix === REF_PREFIX['VARIABLE']) {
return this.variables[index];
}
else if(prefix === REF_PREFIX.REF_CONSTANT) {
else if(prefix === REF_PREFIX['CONSTANT']) {
return this.constants[index];
}
else {
Expand All @@ -224,26 +228,26 @@ AST.prototype = {
},
NewObject: function(type, value) {
// TODO 参数检查
let index = this.refIndexes[REF_PREFIX[`REF_${type}`]];
if(type === OBJECT_TYPE.STRING) {
let index = this.refIndexes[type];
if(type === "STRING") {
this.strings[index] = value;
}
else if(type === OBJECT_TYPE.SLIST) {
else if(type === "SLIST") {
this.slists[index] = value;
}
else if(type === OBJECT_TYPE.SYMBOL) {
else if(type === "SYMBOL") {
this.symbols[index] = value;
}
else if(type === OBJECT_TYPE.VARIABLE) {
else if(type === "VARIABLE") {
this.variables[index] = value;
}
else if(type === OBJECT_TYPE.CONSTANT) {
else if(type === "CONSTANT") {
this.constants[index] = value;
}
else {
throw `type error`;
}
this.refIndexes[REF_PREFIX[`REF_${type}`]]++;
this.refIndexes[type]++;
return makeRef(type, index);
},
};
Expand Down Expand Up @@ -282,9 +286,15 @@ Module.prototype.setASM = function(asmlines) {
}
}

// 线程类
const Thread = function() {
return this;

// 进程状态
const PROCESS_STATE = {
'DEFAULT' : -1, // 默认
'RUNNING' : 1, // 运行
'SLEEPING' : 2, // 睡眠(可中断)
'DEEPSLEEPING': 3, // 深度睡眠(不可中断)
'SUSPENDED' : 4, // 挂起
'DEAD' : 5, // 销毁
};


Expand All @@ -301,5 +311,5 @@ module.exports.getRefType = getRefType;
module.exports.makeRef = makeRef;
module.exports.TypeOfToken = TypeOfToken;
module.exports.Module = Module;
module.exports.Thread = Thread;
module.exports.PROCESS_STATE = PROCESS_STATE;

Loading

0 comments on commit 335c150

Please sign in to comment.