Skip to content

Commit

Permalink
NEW: added method call
Browse files Browse the repository at this point in the history
  • Loading branch information
HolliShake committed Dec 6, 2024
1 parent 2604b80 commit 0ca3772
Show file tree
Hide file tree
Showing 6 changed files with 383 additions and 130 deletions.
11 changes: 11 additions & 0 deletions development.envx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ fn getApiName(a,b,c) {
const PRIVATE_HASH = "qsdad123q3";

fn getHash() {
return PRIVATE_HASH;
}


Expand Down Expand Up @@ -143,3 +144,13 @@ do {
println(qq);
qq += 1;
} while(qq != 200)

println(qq.isEven());

println((2 + 2 + 1).isEven());

println(">>", Number.add(3, 3) * 6);

println("Hello".concat("_" + "World!"));

println(Boolean);
39 changes: 37 additions & 2 deletions src/bytecompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class Visitor {
case Ast.STRING:
this.astString(node);
break;
case Ast.ACCESS:
this.astAccess(node);
break;
case Ast.CALL:
this.astCalll(node);
break;
Expand Down Expand Up @@ -282,14 +285,46 @@ class ByteComiler extends Visitor {
]);
}

astAccess(node) {
this.visit(node.object);

if (node.member.type != Ast.ID) {
throwError(this.parser.tokenizer.envName, this.parser.tokenizer.data, `Expected identifier but got ${node.member.type}`, node.member.position);
}

this.bytecode.push(...[
OPCODE.GET_ATTRIBUTE,
...sliceWord(node.member.value)
]);
}

astCalll(node) {
const args = node.args.reverse();
for (let i = 0; i < args.length; i++) {
this.visit(args[i]);
}
this.visit(node.object);

const isMethodCall = node.object.type == Ast.ACCESS;
if (isMethodCall) {
this.visit(node.object.object);
this.bytecode.push(OPCODE.DUP_TOP);

if (node.object.member.type != Ast.ID) {
throwError(this.parser.tokenizer.envName, this.parser.tokenizer.data, `Expected identifier but got ${node.object.member.type}`, node.member.position);
}

this.bytecode.push(...[
OPCODE.GET_ATTRIBUTE,
...sliceWord(node.object.member.value)
]);
}
else {
this.visit(node.object);
}


this.bytecode.push(...[
OPCODE.CALL_FUNCTION,
isMethodCall ? OPCODE.CALL_METHOD : OPCODE.CALL_FUNCTION,
...slice4Number(args.length)
]);
}
Expand Down
62 changes: 32 additions & 30 deletions src/opcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,38 @@ const OPCODE = Object.freeze({
LOAD_BOOL : 0x11,
LOAD_NULL : 0x12,
LOAD_NAME : 0x13,
CALL_FUNCTION : 0x14,
MAKE_FUNCTION : 0x15,
STORE_FAST : 0x16,
STORE_NAME : 0x17,
STORE_GLOBAL : 0x18,
LOG_NOT : 0x19,
BIT_NOT : 0x1a,
POS : 0x1b,
NEG : 0x1c,
BIN_MUL : 0x1d,
BIN_DIV : 0x1e,
BIN_MOD : 0x1f,
BIN_ADD : 0x20,
BIN_SUB : 0x21,
BIN_SHL : 0x22,
BIN_SHR : 0x23,
BIN_LT : 0x24,
BIN_LE : 0x25,
BIN_GT : 0x26,
BIN_GE : 0x27,
BIN_EQ : 0x28,
BIN_NE : 0x29,
BIN_AND : 0x2a,
BIN_OR : 0x2b,
BIN_XOR : 0x2c,
POP_TOP : 0x2d,
DUP_TOP : 0x2e,
POP_JUMP_IF_FALSE : 0x2f,
JUMP : 0x30,
RETURN : 0x31
GET_ATTRIBUTE : 0x14,
CALL_FUNCTION : 0x15,
CALL_METHOD : 0x16,
MAKE_FUNCTION : 0x17,
STORE_FAST : 0x18,
STORE_NAME : 0x19,
STORE_GLOBAL : 0x1a,
LOG_NOT : 0x1b,
BIT_NOT : 0x1c,
POS : 0x1d,
NEG : 0x1e,
BIN_MUL : 0x1f,
BIN_DIV : 0x20,
BIN_MOD : 0x21,
BIN_ADD : 0x22,
BIN_SUB : 0x23,
BIN_SHL : 0x24,
BIN_SHR : 0x25,
BIN_LT : 0x26,
BIN_LE : 0x27,
BIN_GT : 0x28,
BIN_GE : 0x29,
BIN_EQ : 0x2a,
BIN_NE : 0x2b,
BIN_AND : 0x2c,
BIN_OR : 0x2d,
BIN_XOR : 0x2e,
POP_TOP : 0x2f,
DUP_TOP : 0x30,
POP_JUMP_IF_FALSE : 0x31,
JUMP : 0x32,
RETURN : 0x33
})

const getOpCodeName = (opcode) => {
Expand Down
51 changes: 42 additions & 9 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Ast = Object.freeze({
STRING : "STRING",
BOOL : "BOOL",
NULL : "NULL",
ARRAY : "ARRAY",
ACCESS : "ACCESS",
CALL : "CALL",
INDEX : "INDEX",
Expand Down Expand Up @@ -144,15 +145,50 @@ class Parser {
return null;
}

group() {
if (this.check("(")) {
this.consume("(");
const node = this.mandatory();
this.consume(")");
return node;
}
else if (this.check("[")) {
let start = this.look.position, ended = {};
this.consume("[");
const elements = [];
let element = this.primary();
if (element) {
elements.push(element);
while (this.check(",")) {
this.consume(",");
element = this.primary();
if (!element) {
throwError(this.tokenizer.envName, this.tokenizer.data, `Expected primary but found ${this.look.value}`, this.look.position);
}
elements.push(element);
}
}
this.consume("]");
ended = this.prev.position;
return ({
type: Ast.ARRAY,
elements: elements,
position: mergePos(start, ended)
});
}
return this.terminal();
}

memberOrCall() {
let node = this.terminal();
let node = this.group();
if (!node) {
return node;
}

while (this.check(".") || this.check("(") || this.check("[")) {
if (this.check(".")) {
this.consume(".");

const member = this.terminal();
if (!member) {
throwError(this.tokenizer.envName, this.tokenizer.data, `Expected terminal but found ${this.look.value}`, this.look.position);
Expand Down Expand Up @@ -206,13 +242,7 @@ class Parser {
}

unary() {
if (this.check("(")) {
this.consume("(");
const node = this.mandatory();
this.consume(")");
return node;
}
else if (this.check("!") || this.check("~") || this.check("-") || this.check("+")) {
if (this.check("!") || this.check("~") || this.check("-") || this.check("+")) {
const op = this.look.value;
this.consume(this.look.value);
const node = this.unary();
Expand Down Expand Up @@ -755,6 +785,7 @@ class Parser {
}
}
this.consume(";");
ended = this.prev.position;
return ({
type: Ast.VAR,
declarations: declarations,
Expand Down Expand Up @@ -804,6 +835,7 @@ class Parser {
}
}
this.consume(";");
ended = this.prev.position;
return ({
type: Ast.CONST,
declarations: declarations,
Expand Down Expand Up @@ -853,6 +885,7 @@ class Parser {
}
}
this.consume(";");
ended = this.prev.position;
return ({
type: Ast.LOCAL,
declarations: declarations,
Expand Down
Loading

0 comments on commit 0ca3772

Please sign in to comment.