funcs, import

This commit is contained in:
jusax23 2022-10-21 23:57:06 +02:00
parent 16e08bded6
commit 389afbe8e5
17 changed files with 1344 additions and 889 deletions

4
.gitignore vendored
View file

@ -3,6 +3,10 @@ test.lisp
test.mms test.mms
test.mmo test.mmo
testfun.lisp
testfun.mms
testfun.mmo
# Logs # Logs
logs logs
*.log *.log

128
js/ctx.js
View file

@ -1,11 +1,16 @@
import { link } from "fs";
import { error } from "./lexer.js"; import { error } from "./lexer.js";
let count = 0; let count = 0;
export class context{ export class context{
#list = {}; #list = {
V:[]
};
#functions = {
};
#types = { #types = {
v: { size: 1,type: 0, content: false },
u8: { size: 1,type: 0, content: false }, u8: { size: 1,type: 0, content: false },
u16: { size: 2,type: 0, content: false }, u16: { size: 2,type: 0, content: false },
u32: { size: 4,type: 0, content: false }, u32: { size: 4,type: 0, content: false },
@ -16,29 +21,56 @@ export class context{
i64: { size: 8,type: 1, content: false }, i64: { size: 8,type: 1, content: false },
f32: { size: 4,type: 2, content: false }, f32: { size: 4,type: 2, content: false },
f64: { size: 8,type: 2, content: false }, f64: { size: 8,type: 2, content: false },
void: {link: "v"},
char: {link:"u8"}, char: {link:"u8"},
c: {link:"u8"}, c: {link:"u8"},
bool: {link:"u8"}, bool: {link:"u8"},
boolean: {link:"bool"},
b: {link:"bool"}, b: {link:"bool"},
uint8_t: { link:"u8" },
uint16_t: { link:"u16" },
uint32_t: { link:"u32" },
uint64_t: { link:"u64" },
int8_t: { link:"i8" },
int16_t: { link:"i16" },
int32_t: { link:"i32" },
int64_t: { link:"i64" },
float: { link:"f32" },
double: { link:"f64" },
}; };
#upper = []; #upper = [];
constructor(upper = null){ #lower = [];
#isLocal = false;
constructor(upper = null,local = false){
this.#upper = upper; this.#upper = upper;
this.#isLocal = local;
} }
nextLevel(){ get local(){
return new context(this); return this.#isLocal;
}
nextLevel(local = true){
let newCon = new context(this,local||this.#isLocal);
if(!local)this.#lower.push(newCon);
return newCon;
} }
add({name,vType,size,amount=1,type = 0, content = 0,config = [1]}){ add({name,vType,size,amount=1,type = 0, content = 0,config = [1]}){
if (!this.#list[vType]) this.#list[vType] = {}; if (!this.#list[vType]) this.#list[vType] = [];
this.#list[vType][name+""] = {name: vType+(count++),size,amount,type,content,config}; let nowid = this.#list[vType].push({varName:name+"",name: vType+(count++),size,amount,type,content,config,pos:()=>{
return this.#list[vType].reduce((v,e,i)=>v+(i<nowid?(e.size*e.amount):0),0);
},used:false})-1;
return this.#list[vType][this.#list[vType].length-1];
} }
find(name, vType,pos=name.pos,quit=true){ find(name, vType,pos=name.pos,quit=true){
let elem = (this.#list[vType]??{})[name+""]??null; let nowId = (this.#list[vType] ?? []).findIndex((v)=>{
if(v.varName==name+"")return true;
if (!elem && this.#upper){ });
//let elem = (this.#list[vType]??{})[name+""]??null;
let elem = (this.#list[vType] ?? [])[nowId]??null;
if (nowId==-1 && this.#upper){
elem = this.#upper.find(name+"",vType,pos,false); elem = this.#upper.find(name+"",vType,pos,false);
} }
if(!elem&&quit) error("Can not find '"+name+"' in context!",...pos); if(!elem&&quit) error("Can not find '"+name+"' in Variable context!",...pos);
elem.used = true;
return elem; return elem;
} }
@ -48,40 +80,68 @@ export class context{
if(!type){ if(!type){
if(this.#upper) type = this.#upper.getType(name+"", pos, false); if(this.#upper) type = this.#upper.getType(name+"", pos, false);
if (!type) error("Can not find '" + name + "' in context", ...pos); if (!type) error("Can not find '" + name + "' in Type context", ...pos);
return type; return type;
}else{ }else{
if(type.link){ if(type.link){
type = this.getType(type.link,pos,false); type = this.getType(type.link,pos,false);
if (!type) error("Can not find '" + name + "' in context", ...pos); if (!type) error("Can not find '" + name + "' in Type context", ...pos);
return type; return type;
}else{ }else{
return type; return type;
} }
} }
/*do{
if (type) type = this.#types[type.link]
else type = this.#types[name];
}while(type&&type.link);*/
} }
addLinkType(name,link){ addLinkType(name,link){
this.#types[name] = {link}; this.#types[name] = {link};
} }
size(){
let size = Object.entries(this.#list).reduce((va,tt)=>va+tt[1].reduce((v, e) => v + (e.size * e.amount), 0),0);
size+=8-size%8;
return size;
}
build(){ addFunction({ name, code, type = 0,args=[] }){
let out = ` this.#functions[name+""] = {name:"F"+(count++),code,type,args,used:false};
LOC Data_Segment return this.#functions[name+""];
GREG @ }
findFunction(name){
let elem = this.#functions[name+""] ?? null;
if (!elem && this.#upper) {
elem = this.#upper.findFunction(name);
}
if (!elem) return false;
elem.used = true;
return elem;
}
buildFunctions(){
let code = "";
for(let funName in this.#functions){
const fun = this.#functions[funName];
if(!fun.used)continue;
code += `
${fun.name} SWYM
${fun.code}
`; `;
}
for (let i = 0; i < this.#lower.length; i++) {
const e = this.#lower[i];
code+=e.buildFunctions();
}
return code;
}
build(varsonly=false){
if(this.#isLocal) return "";
let out = ` LOC Data_Segment
GREG @
`;
if(varsonly)out="";
for(let vType in this.#list){ for(let vType in this.#list){
for(let UName in this.#list[vType]){ for(let id = 0;id<this.#list[vType].length;id++){
let { size, amount, type, content, name } = this.#list[vType][UName]; let { size, amount, type, content, name ,used} = this.#list[vType][id];
if(!used)continue;
if (!isNaN(content)) content = Number(content); if (!isNaN(content)) content = Number(content);
if (size <= 8 && amount == 1) { if (size <= 8 && amount == 1) {
out += `${name} ${(["BYTE", "WYDE", "TETRA", "TETRA", "OCTA", "OCTA", "OCTA", "OCTA"])[size - 1]} ${content}\n`; out += `${name} ${(["BYTE", "WYDE", "TETRA", "TETRA", "OCTA", "OCTA", "OCTA", "OCTA"])[size - 1]} ${content}\n`;
@ -91,9 +151,19 @@ ${name} BYTE ${content}
LOC ${name}+${size * amount + 1}\n`; LOC ${name}+${size * amount + 1}\n`;
} }
} }
} }
out +=" LOC #100\nMain SWYM\n"; for (let i = 0; i < this.#lower.length; i++) {
const e = this.#lower[i];
out+=e.build(true);
}
if(varsonly)return out
out += "intMask OCTA #7fffffffffffffff\n";
out += "HEAPpoint OCTA 0\n";
out += "HEAP BYTE 0\n";
out += " LOC #100\n"
out+= "Main LDA $0,HEAP\n";
out+= " STOU $0,HEAPpoint\n";
return out; return out;
} }
} }

View file

@ -1,7 +1,7 @@
import { context } from "./ctx.js"; import { context } from "./ctx.js";
import { createType, error } from "./lexer.js"; import { createType, error } from "./lexer.js";
import nativefunc from "./nativefunc.js"; import nativefunc from "./nativefunc.js";
import { COMPUTE_TYPES } from "./types.js"; import { COMPUTE_TYPES, convertType } from "./types.js";
let nid = 0; let nid = 0;
@ -10,6 +10,43 @@ export function execute({ data, target = 0, ctx = new context()}){
if(target > 255) error("To much registers are required to run this. Support for this case will be build in later!",...data.pos); if(target > 255) error("To much registers are required to run this. Support for this case will be build in later!",...data.pos);
let [type, d] = createType(data); let [type, d] = createType(data);
if(type == "code"){ if(type == "code"){
let userFunc = ctx.findFunction(data[0]);
if(userFunc){
let params = data.array.slice(1,userFunc.args.length+1).map((d, i) => execute({ data: d, target,ctx}));
if(params.length<userFunc.args.length)error("function '"+data[0]+"' is called with to less Arguments!",...data[data.length-1].pos);
let ctxSize = ctx.size();
let code = `
${params.map((p,i)=>{
let arg = userFunc.args[i];
return [
p.code,
convertType(p.type,arg.type,target),
` LDOU $${target + 1},HEAPpoint`,
` SET $${target + 2},${ctxSize+arg.pos()}`,
` ${arg.type == COMPUTE_TYPES.FLOAT?(arg.size > 4 ? "STOU" : "STSF"):("ST"+("BWTTOOOO")[arg.size - 1])+(arg.type == 0 ? "U" : "")} $${target},$${target + 1},$${target + 2}`
]
}).flat(Infinity).join("\n")}
LDOU $${target},HEAPpoint
ADDU $${target},$${target},${ctxSize}
STOU $${target},HEAPpoint
GET $${target},rJ
PUSHJ $${target+1},${userFunc.name}
PUT rJ,$${target}
LDOU $${target},HEAPpoint
SUBU $${target},$${target},${ctxSize}
STOU $${target},HEAPpoint
SET $${target},$${target+1}
`;
return {
code,
type:userFunc.type.type
}
}
try { try {
let { type, code } = nativefunc[data[0]]({ let { type, code } = nativefunc[data[0]]({
execute: ({ data, target = 0, ctx:contx = ctx }) => execute({ data, target, ctx:contx }), execute: ({ data, target = 0, ctx:contx = ctx }) => execute({ data, target, ctx:contx }),
@ -24,34 +61,74 @@ export function execute({ data, target = 0, ctx = new context()}){
error(`'${data[0]}' is not a function`,...data.pos); error(`'${data[0]}' is not a function`,...data.pos);
} }
}else if (type == "var"){ }else if (type == "var"){
let { size, amount, type, name } = ctx.find(d,"V",data.pos); let { size, amount, type, name,pos } = ctx.find(d,"V",data.pos);
if (size <= 8 && amount == 1){ if(ctx.local){
if(type == COMPUTE_TYPES.FLOAT){ if (size <= 8 && amount == 1) {
return { if (type == COMPUTE_TYPES.FLOAT) {
type: 2, return {
code: ` ${size > 4 ? "LDOU" :"LDSF"} $${target},${name}` type: 2,
code: ` LDOU $${target},HEAPpoint
SET $${target + 1},${pos()}
${size > 4 ? "LDOU" : "LDSF"} $${target},$${target},$${target+1}`
}
} else {
return {
type: type,
code: ` LDOU $${target },HEAPpoint
SET $${target + 1},${pos()}
LD${("BWTTOOOO")[size - 1]}${type == 0 ? "U" : ""} $${target},$${target},$${target + 1}`
}
} }
}else{ } else {
return { return {
type: type, type: 0,
code: ` LD${(["B", "W", "T", "T", "O", "O", "O", "O"])[size-1]}${type==0?"U":""} $${target},${name}` code: ` LDOU $${target},HEAPpoint
SET $${target + 1},${pos()}
ADDU $${target},$${target},$${target + 1}`
} }
} }
}else{ }else{
return { if (size <= 8 && amount == 1) {
type: 0, if (type == COMPUTE_TYPES.FLOAT) {
code: ` LDA $${target},${name}` return {
type: 2,
code: ` ${size > 4 ? "LDOU" : "LDSF"} $${target},${name}`
}
} else {
return {
type: type,
code: ` LD${("BWTTOOOO")[size - 1]}${type == 0 ? "U" : ""} $${target},${name}`
}
}
} else {
return {
type: 0,
code: ` LDA $${target},${name}`
}
} }
} }
}else if (type == "bool"){ }else if (type == "bool"){
return { return {
code: ` SET $${target},${d}`, code: ` SET $${target},${d}`,
type: COMPUTE_TYPES.UINT type: COMPUTE_TYPES.UINT
} }
}else if (type == "num"){ }else if (type == "num"){
let hex = "";
if(Number.isInteger(d)){
hex = d;
}else{
var buf = new ArrayBuffer(8);
(new Float64Array(buf))[0] = d;
let ddd = (new Uint32Array(buf));
hex ="#";
hex+=ddd[0].toString(16);
hex+=ddd[1].toString(16);
}
return { return {
code: ` SET $${target},${d}`, code: ` SET $${target},${hex}`,
type: COMPUTE_TYPES.UINT type: COMPUTE_TYPES.UINT
} }
}else if (type == "str"){ }else if (type == "str"){

View file

@ -1,33 +1,91 @@
import { argsCount } from "../errors.js"; import { argsCount } from "../errors.js";
import { createType, error } from "../lexer.js";
export default { export default {
progn: ({ execute, data, target, nid, ctx }) => { progn: ({ execute, data, target, nid, ctx }) => {
if (data.length < 2) argsCount("progn", 1, data.pos); if (data.length < 2) argsCount("progn", 1, data.pos);
let newctx = ctx.nextLevel(); let newctx = ctx.nextLevel(false);
let content = data.array.slice(1).map(d => execute({ data: d, target: target, ctx: newctx }).code).join("\n")
return { return {
type: 0, type: 0,
code: `//new code Block code: `//new code Block
${data.array.slice(1).map(d => execute({ data: d, target, ctx: newctx })).join("\n")} ${content}
//end Code Block` //end Code Block`
} }
}, },
defun: ({ execute, data, target, nid, ctx }) => {
if(data.length < 5)argsCount("defun",4,...data[0].pos);
let newctx = ctx.nextLevel(true);
let type = ctx.getType(data[2]);
let [argType,argsList] = createType(data[3]);
if(argType!="code")error("The third Argument of defun must contain the args!",...data[3].pos);
let args = [];
if(argsList.length%2!=0)error("The Argument List must cotain an even amount of Names + Types",...data[3].pos);
for (let i = 0; i < argsList.length; i+=2) {
let name = argsList[i];
let type = ctx.getType(argsList[i+1]);
args.push(newctx.add({name,vType:"V",size:8,type:type.type}));
}
let fun = ctx.addFunction({name:data[1],code:"",type,args:args});
let code = data.array.slice(4).map(l=>{
let {type,code} = execute({data:l,target:0,ctx:newctx});
return code;
}).join("\n");
fun.code = code;
fun.code += "\n POP 1,0";
return {
code:"",
type: 0
};
},
return:({ execute, data, target, nid, ctx })=>{
if(data.length == 1){
return {
type:0,
code:` POP 1,0
`
}
}
let {type,code} = execute({ data: data[1], target:0});
return {
type:0,
code:`
${code}
POP 1,0
`
}
},
if: ({ execute, data, target, nid, ctx }) => { if: ({ execute, data, target, nid, ctx }) => {
if (data.length < 3) argsCount("if", 2, data.pos); if (data.length < 3) argsCount("if", 2, data.pos);
let condition = execute({ data: data[1], target }); let condition = execute({ data: data[1], target });
let id1 = nid(); let id1 = nid();
let id2 = nid(); let id2 = nid();
if(data.length == 3){
return {
type: 0,
code: `//if
${condition.code}
BZ $${target},fi${id1}
${execute({ data: data[2],target }).code}
fi${id1} SWYM`
}
}
return { return {
type: 0, type: 0,
code: `//if code: `//if
${condition.code} ${condition.code}
BZ $${target},else${id1} BZ $${target},else${id1}
${execute({ data: data[2] }).code} ${execute({ data: data[2],target }).code}
${data.length > 3 ? "JMP fi" + id2 : ""} JMP fi${id2}
else${id1} SWYM else${id1} SWYM
${data.length > 3 ? execute({ data: data[2] }).code : ""} ${data.length > 3 ? execute({ data: data[2],target }).code : ""}
fi${id2} SWYM fi${id2} SWYM`
`
} }
}, },
}; };

View file

@ -1,10 +1,11 @@
import { argsCount } from "../errors.js"; import { argCount, argsCount } from "../errors.js";
import { error } from "../lexer.js";
import { convertType, getOutType } from "../types.js" import { convertType, getOutType } from "../types.js"
export default { export default {
"+": ({ execute, data, target, nid, ctx }) => { "+": ({ execute, data, target, nid, ctx }) => {
if (data.length < 3) argsCount("+",2,data.pos); if (data.length < 3) argsCount("+",2,data.pos);
let params = data.array.slice(1).map((d, i) => execute({ data: d, target: target + (i ? 0 : 1) })); let params = data.array.slice(1).map((d, i) => execute({ data: d, target: target + (i==0 ? 0 : 1) }));
let outType = getOutType(...params.map(d => d.type)); let outType = getOutType(...params.map(d => d.type));
return { return {
type: outType, type: outType,
@ -23,7 +24,7 @@ export default {
}, },
"-": ({ execute, data, target, nid, ctx }) => { "-": ({ execute, data, target, nid, ctx }) => {
if (data.length < 3) argsCount("-", 2, data.pos); if (data.length < 3) argsCount("-", 2, data.pos);
let params = data.array.slice(1).map((d, i) => execute({ data: d, target: target + (i ? 0 : 1) })); let params = data.array.slice(1).map((d, i) => execute({ data: d, target: target + (i==0 ? 0 : 1) }));
let outType = getOutType(...params.map(d => d.type)); let outType = getOutType(...params.map(d => d.type));
return { return {
type: outType, type: outType,
@ -42,7 +43,7 @@ export default {
}, },
"*": ({ execute, data, target, nid, ctx }) => { "*": ({ execute, data, target, nid, ctx }) => {
if (data.length < 3) argsCount("*", 2, data.pos); if (data.length < 3) argsCount("*", 2, data.pos);
let params = data.array.slice(1).map((d, i) => execute({ data: d, target: target + (i ? 0 : 1) })); let params = data.array.slice(1).map((d, i) => execute({ data: d, target: target + (i==0 ? 0 : 1) }));
let outType = getOutType(...params.map(d => d.type)); let outType = getOutType(...params.map(d => d.type));
return { return {
type: outType, type: outType,
@ -61,7 +62,7 @@ export default {
}, },
"/": ({ execute, data, target, nid, ctx }) => { "/": ({ execute, data, target, nid, ctx }) => {
if (data.length < 3) argsCount("/", 2, data.pos); if (data.length < 3) argsCount("/", 2, data.pos);
let params = data.array.slice(1).map((d, i) => execute({ data: d, target: target + (i ? 0 : 1) })); let params = data.array.slice(1).map((d, i) => execute({ data: d, target: target + (i==0 ? 0 : 1) }));
let outType = getOutType(...params.map(d => d.type)); let outType = getOutType(...params.map(d => d.type));
return { return {
type: outType, type: outType,
@ -78,4 +79,77 @@ export default {
}).flat(Infinity).join("\n") }).flat(Infinity).join("\n")
} }
}, },
"round": ({ execute, data, target, nid, ctx }) => {
if (data.length < 2) argCount("round", 1, data.pos);
let param = execute({ data: data[1], target: target });
return {
type: 1,
code: `${param.code}
${convertType(param.type,2,target)}
FINT $${target}$${target}`
}
},
"sqrt": ({ execute, data, target, nid, ctx }) => {
if (data.length < 2) argCount("sqrt", 1, data.pos);
let param = execute({ data: data[1], target: target });
return {
type: 1,
code: `${param.code}
${convertType(param.type,2,target)}
FSQRT $${target}$${target}`
}
},
"uint": ({ execute, data, target, nid, ctx }) => {
if (data.length < 2) argCount("int", 1, data.pos);
let param = execute({ data: data[1], target: target });
return {
type: 1,
code: `${param.code}
${convertType(param.type,0,target)}`
}
},
"int": ({ execute, data, target, nid, ctx }) => {
if (data.length < 2) argCount("uint", 1, data.pos);
let param = execute({ data: data[1], target: target });
return {
type: 1,
code: `${param.code}
${convertType(param.type,1,target)}`
}
},
"float": ({ execute, data, target, nid, ctx }) => {
if (data.length < 2) argCount("float", 1, data.pos);
let param = execute({ data: data[1], target: target });
return {
type: 1,
code: `${param.code}
${convertType(param.type,2,target)}`
}
},
"mod": ({ execute, data, target, nid, ctx }) => {
if (data.length < 3) argCount("mod", 2, data.pos);
let param1 = execute({ data: data[1], target });
let param2 = execute({ data: data[2], target: target+1 });
let outType = getOutType(param1.type,param2.type);
let code = `${param1.code}
${convertType(param1.type,outType,target)}
${param2.code}
${convertType(param2.type,outType,target+1)}
`;
if(outType == 0){
code+=` DIVU $${target},$${target},$${target+1}
GET $${target},rR`;
}else if(outType == 1){
code+=` DIV $${target},${target},$${target+1}
GET $${target},rR`;
}else if(outType == 2){
code+=` FREM $${target},$${target},$${target+1}`;
}else{
error("Unexpected Type: "+outType,...data[1].pos);
}
return{
type:outType,
code
}
},
} }

View file

@ -1,5 +1,3 @@
export default { export default {
assm: ({ execute, data, target, nid, ctx }) => { assm: ({ execute, data, target, nid, ctx }) => {
let cmds = data.array.slice(1, -1); let cmds = data.array.slice(1, -1);
@ -26,6 +24,15 @@ export default {
}, },
addr: ({ execute, data, target, nid, ctx }) => { addr: ({ execute, data, target, nid, ctx }) => {
let nv = ctx.find(data[1], "V"); let nv = ctx.find(data[1], "V");
if(ctx.local){
return {
code: ` LDOU $${target},HEAPpoint
SET $${target + 1},${nv.pos()}
ADDU $${target},$${target},$${target + 1 }`,
type: 0
};
}
return { return {
code: ` LDA $${target},${nv.name}`, code: ` LDA $${target},${nv.name}`,
type: 0 type: 0

View file

@ -1,7 +1,7 @@
import { createType, error, LISPcmd } from "../lexer.js"; import { createType, error, LISPcmd } from "../lexer.js";
import { COMPUTE_TYPES, convertType } from "../types.js"; import { COMPUTE_TYPES, convertType } from "../types.js";
export default { const vars = {
defvar: ({ execute, data, target, nid, ctx }) => { defvar: ({ execute, data, target, nid, ctx }) => {
let param = data[3]; let param = data[3];
let [type, d] = createType(param); let [type, d] = createType(param);
@ -9,10 +9,32 @@ export default {
let varType = ctx.getType(data[2]); let varType = ctx.getType(data[2]);
if (varType.content) error("A variable only can be created with a primitive Type.", ...data[2].pos); if (varType.content) error("A variable only can be created with a primitive Type.", ...data[2].pos);
if (type == "var" || type == "code") { if (type == "var" || type == "code" || ctx.local) {
error("devfar with input is not implemented yet.", ...param.pos) ctx.add({ name: data[1], vType: "V", size: varType.size, amount: 1, type: varType.type, content: 0 });
let{code,type} = vars.set({execute,data:["set", data[1], data[3]],target,nid,ctx});
return {
code: code,
type: type
}
} else { } else {
ctx.add({ name: data[1], vType: "V", size: varType.size, amount: 1, type: varType.type, content: param }); let content = "0";
if(type == "num"){
if(Number.isInteger(d)){
content = d;
}else{
var buf = new ArrayBuffer(8);
(new Float64Array(buf))[0] = d;
let ddd = (new Uint32Array(buf));
content ="#";
content+=ddd[1].toString(16);
content+=ddd[0].toString(16);
}
}else{
content=param;
}
ctx.add({ name: data[1], vType: "V", size: varType.size, amount: 1, type: varType.type, content });
return { return {
code: "", code: "",
type: 0 type: 0
@ -22,23 +44,45 @@ export default {
set: ({ execute, data, target, nid, ctx }) => { set: ({ execute, data, target, nid, ctx }) => {
let toSet = ctx.find(data[1],"V"); let toSet = ctx.find(data[1],"V");
let { code, type } = execute({ data: data[2], target }); let { code, type } = execute({ data: data[2], target });
if(toSet.type == COMPUTE_TYPES.FLOAT){ if(ctx.local){
return { if (toSet.type == COMPUTE_TYPES.FLOAT) {
code: `${code} return {
code: `${code}
${convertType(type, toSet.type, target)}
LDOU $${target + 1},HEAPpoint
SET $${target + 2},${toSet.pos()}
${toSet.size > 4 ? "STOU" : "STSF"} $${target},$${target+1},$${target + 2}`,
type: toSet.type
};
} else {
return {
code: `${code}
${convertType(type, toSet.type, target)}
LDOU $${target + 1},HEAPpoint
SET $${target + 2},${toSet.pos()}
ST${("BWTTOOOO")[toSet.size - 1]}${toSet.type == 0 ? "U" : ""} $${target},$${target + 1},$${target + 2}`,
type: toSet.type
};
}
}else{
if (toSet.type == COMPUTE_TYPES.FLOAT) {
return {
code: `${code}
${convertType(type, toSet.type, target)} ${convertType(type, toSet.type, target)}
${toSet.size > 4 ? "STOU" : "STSF"} $${target},${toSet.name}`, ${toSet.size > 4 ? "STOU" : "STSF"} $${target},${toSet.name}`,
type: toSet.type type: toSet.type
}; };
}else{ } else {
return { return {
code: `${code} code: `${code}
${convertType(type, toSet.type, target)} ${convertType(type, toSet.type, target)}
ST${(["B", "W", "T", "T", "O", "O", "O", "O"])[toSet.size - 1]}${type == 0 ? "U" : ""} $${target},${toSet.name}`, ST${("BWTTOOOO")[toSet.size - 1]}${toSet.type == 0 ? "U" : ""} $${target},${toSet.name}`,
type: toSet.type type: toSet.type
}; };
}
} }
}, },
defarr: ({ execute, data, target, nid, ctx }) => { defarr: ({ execute, data, target, nid, ctx }) => {
let param = data.array.slice(3, -1).map(d => Number(d)); let param = data.array.slice(3, -1).map(d => Number(d));
@ -53,4 +97,17 @@ ${convertType(type, toSet.type, target)}
type: 0 type: 0
}; };
}, },
} readarr: ({ execute, data, target, nid, ctx })=>{
let arr = ctx.find(data[1],"V");
if (arr.config.length > data.length - 2) error(data[1] + " is a " + arr.config.length +"dim Array. You have to provide at least this much Arguments beside the Array name!",...data[1].pos);
let params = data.array.slice(2).map((d, i) => execute({ data: d, target: target }));
return{
code:`
`,
type:arr.type
}
}
};
export default vars;

37
js/preprocessor.js Normal file
View file

@ -0,0 +1,37 @@
import * as path from "path";
function join(p1,p2){
if(p2.startsWith("/"))return p2;
return path.join(p1,"..",p2);
}
function unComment(str){
let out = "";
let inK = false;
for (let i = 0; i < str.length; i++) {
if(str[i] == "\\"){
out+=str[i+1]??"";
i+=2;
}else if(str[i] == "\""){
inK = !inK;
out += str[i];
}else if(str[i] == ";" || str[i] == " "){
if(inK)out += str[i];
else break;
}else{
out += str[i];
}
}
return out.trim();
}
export default (str,filename,readFile)=>str.split("\n").map(d=>d.trim()).map((l,i)=>{
if(!l.startsWith("#"))return l;
if(l.startsWith("#import")){
let toImport = unComment(l.substring(8).trim());
if(toImport.startsWith("\"") && toImport.startsWith("\"")){
return readFile(join(filename,toImport.slice(1,-1)),i);
}else{
return readFile("lisp/"+toImport+".lisp",i);
}
}
}).join("\n");

View file

@ -14,25 +14,25 @@ export const convertType = (typein,typeout,reg) => {
return "//no type conversion nessesary (u -> i)"; return "//no type conversion nessesary (u -> i)";
} }
if(typein == COMPUTE_TYPES.INT && typeout == COMPUTE_TYPES.UINT){ if(typein == COMPUTE_TYPES.INT && typeout == COMPUTE_TYPES.UINT){
return `SET $${reg + 1},#7fffffffffffffff return ` LDOU $${reg + 1},intMask
AND $${reg},$${reg},$${reg + 1} convert i -> u`; AND $${reg},$${reg},$${reg + 1} //convert i -> u`;
} }
if(typein == COMPUTE_TYPES.INT && typeout == COMPUTE_TYPES.FLOAT){ if(typein == COMPUTE_TYPES.INT && typeout == COMPUTE_TYPES.FLOAT){
return ` FLOT $${reg},$${reg} convert i -> f`; return ` FLOT $${reg},$${reg} //convert i -> f`;
} }
if(typein == COMPUTE_TYPES.UINT && typeout == COMPUTE_TYPES.FLOAT){ if(typein == COMPUTE_TYPES.UINT && typeout == COMPUTE_TYPES.FLOAT){
return `SET $${reg + 1},#7fffffffffffffff return ` LDOU $${reg + 1},intMask
AND $${reg},$${reg},$${reg + 1} AND $${reg},$${reg},$${reg + 1}
FLOT $${reg},$${reg} convert i -> f`; FLOT $${reg},$${reg} //convert u -> f`;
} }
if(typein == COMPUTE_TYPES.FLOAT && typeout == COMPUTE_TYPES.INT){ if(typein == COMPUTE_TYPES.FLOAT && typeout == COMPUTE_TYPES.INT){
return ` FIX $${reg},$${reg} convert f -> i`; return ` FIX $${reg},$${reg} //convert f -> i`;
} }
if(typein == COMPUTE_TYPES.FLOAT && typeout == COMPUTE_TYPES.UINT){ if(typein == COMPUTE_TYPES.FLOAT && typeout == COMPUTE_TYPES.UINT){
return ` FLOT $${reg},$${reg} convert i -> f return ` FIX $${reg},$${reg} //convert f -> u
SET $${reg + 1},#7fffffffffffffff LDOU $${reg + 1},intMask
AND $${reg},$${reg},$${reg+1}`; AND $${reg},$${reg},$${reg+1}`;
} }
error("[System error] Could not find a possible Type conversion.") error("[System error] Could not find a possible Type conversion. ("+typein+", "+typeout+")");
} }

3
lisp/math.lisp Normal file
View file

@ -0,0 +1,3 @@
(defvar pi:f64 3.14159265358979323846)
(defvar e:f64 2.71828182845904523536)

13
main.js
View file

@ -1,7 +1,8 @@
import * as fs from "fs"; import * as fs from "fs";
import { context } from "./js/ctx.js"; import { context } from "./js/ctx.js";
import { execute } from "./js/execute.js"; import { execute } from "./js/execute.js";
import { LISPcmd, LISPstring, createType } from "./js/lexer.js"; import { LISPcmd, LISPstring, createType, error } from "./js/lexer.js";
import preprocessor from "./js/preprocessor.js";
var path = process.argv[2]; var path = process.argv[2];
var pathout = process.argv[3]; var pathout = process.argv[3];
if (!path || !pathout) { if (!path || !pathout) {
@ -10,7 +11,14 @@ if (!path || !pathout) {
} }
var file = fs.readFileSync(path).toString(); var file = fs.readFileSync(path).toString();
var data = new LISPcmd("(\n" + file + "\n)", 0, 0); let strcode = preprocessor(file,path,(path,line)=>{
try{
return fs.readFileSync(path).toString();
}catch(_){
error("Can not import file: "+path+" -> "+_, line, 0);
}
});
var data = new LISPcmd("(\n" + strcode + "\n)", 0, 0);
let code = ""; let code = "";
@ -23,5 +31,6 @@ for (var i = 0; i < data.length; i++) {
let result = ctx.build(); let result = ctx.build();
result+=code; result+=code;
result+=ctx.buildFunctions();
fs.writeFileSync(pathout, result); fs.writeFileSync(pathout, result);
console.log(`Finished compiling in ${Math.round(performance.now()) / 1000}sec. Assembly saved to: ${pathout}`); console.log(`Finished compiling in ${Math.round(performance.now()) / 1000}sec. Assembly saved to: ${pathout}`);

60
package-lock.json generated
View file

@ -9,13 +9,44 @@
"version": "0.0.1", "version": "0.0.1",
"license": "UNLICENSED", "license": "UNLICENSED",
"dependencies": { "dependencies": {
"fs": "^0.0.1-security" "fs": "^0.0.1-security",
"path": "^0.12.7"
} }
}, },
"node_modules/fs": { "node_modules/fs": {
"version": "0.0.1-security", "version": "0.0.1-security",
"resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
"integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==" "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w=="
},
"node_modules/inherits": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
"integrity": "sha512-x00IRNXNy63jwGkJmzPigoySHbaqpNuzKbBOmzK+g2OdZpQ9w+sxCN+VSB3ja7IAge2OP2qpfxTjeNcyjmW1uw=="
},
"node_modules/path": {
"version": "0.12.7",
"resolved": "https://registry.npmjs.org/path/-/path-0.12.7.tgz",
"integrity": "sha512-aXXC6s+1w7otVF9UletFkFcDsJeO7lSZBPUQhtb5O0xJe8LtYhj/GxldoL09bBj9+ZmE2hNoHqQSFMN5fikh4Q==",
"dependencies": {
"process": "^0.11.1",
"util": "^0.10.3"
}
},
"node_modules/process": {
"version": "0.11.10",
"resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz",
"integrity": "sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==",
"engines": {
"node": ">= 0.6.0"
}
},
"node_modules/util": {
"version": "0.10.4",
"resolved": "https://registry.npmjs.org/util/-/util-0.10.4.tgz",
"integrity": "sha512-0Pm9hTQ3se5ll1XihRic3FDIku70C+iHUdT/W926rSgHV5QgXsYbKZN8MSC3tJtSkhuROzvsQjAaFENRXr+19A==",
"dependencies": {
"inherits": "2.0.3"
}
} }
}, },
"dependencies": { "dependencies": {
@ -23,6 +54,33 @@
"version": "0.0.1-security", "version": "0.0.1-security",
"resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
"integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==" "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w=="
},
"inherits": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
"integrity": "sha512-x00IRNXNy63jwGkJmzPigoySHbaqpNuzKbBOmzK+g2OdZpQ9w+sxCN+VSB3ja7IAge2OP2qpfxTjeNcyjmW1uw=="
},
"path": {
"version": "0.12.7",
"resolved": "https://registry.npmjs.org/path/-/path-0.12.7.tgz",
"integrity": "sha512-aXXC6s+1w7otVF9UletFkFcDsJeO7lSZBPUQhtb5O0xJe8LtYhj/GxldoL09bBj9+ZmE2hNoHqQSFMN5fikh4Q==",
"requires": {
"process": "^0.11.1",
"util": "^0.10.3"
}
},
"process": {
"version": "0.11.10",
"resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz",
"integrity": "sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A=="
},
"util": {
"version": "0.10.4",
"resolved": "https://registry.npmjs.org/util/-/util-0.10.4.tgz",
"integrity": "sha512-0Pm9hTQ3se5ll1XihRic3FDIku70C+iHUdT/W926rSgHV5QgXsYbKZN8MSC3tJtSkhuROzvsQjAaFENRXr+19A==",
"requires": {
"inherits": "2.0.3"
}
} }
} }
} }

View file

@ -3,10 +3,10 @@
"version": "0.0.1", "version": "0.0.1",
"description": "", "description": "",
"main": "main.js", "main": "main.js",
"type":"module", "type": "module",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"run": "node . test.lisp test.mms && mmixal test.mms && mmix test.mmo" "run": "node . testfun.lisp testfun.mms && mmixal testfun.mms && mmix testfun.mmo"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -15,6 +15,7 @@
"author": "jusax23", "author": "jusax23",
"license": "UNLICENSED", "license": "UNLICENSED",
"dependencies": { "dependencies": {
"fs": "^0.0.1-security" "fs": "^0.0.1-security",
"path": "^0.12.7"
} }
} }