Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/antlr4/PiccodeScript.g4
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ symbol_lift
symbol_entry
: ID (symbol_lift)? ;

declaration: ID CC (module | func);
declaration: ID CC (module | func | import_module);

module:
MODULE LBRACE module_stmts RBRACE;
Expand Down Expand Up @@ -102,11 +102,12 @@ closure_decl: BOR arg_list? BOR ARROW expr;
unary:
EXCLAIM expr
| SUB expr
| RETURN expr
| TILDE expr
| BAND expr;

if_expr:
IF expr LBRACE expr* RBRACE (ELSE LBRACE expr* RBRACE)?;
IF expr LBRACE expr RBRACE (ELSE LBRACE expr RBRACE)?;

when_expr:
WHEN expr LBRACE when_cases else_case? RBRACE;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/piccode/ast/Arg.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
public class Arg extends Ast {
public String name;
public Ast def_val;
public boolean export;

public Arg(String name, Ast def_val) {
this.name = name;
this.def_val = def_val;
this.export = false;
}

public Arg(String name) {
this.name = name;
this.def_val = null;
this.export = false;
}

@Override
Expand Down
37 changes: 28 additions & 9 deletions src/main/java/org/piccode/ast/CCOperationAst.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.piccode.ast;

import com.github.tomaslanger.chalk.Chalk;
import java.util.concurrent.ExecutionException;
import org.piccode.piccodescript.TargetEnvironment;
import org.piccode.rt.Context;
Expand Down Expand Up @@ -34,18 +35,37 @@ public String toString() {

@Override
public PiccodeValue execute(Integer frame) {
if (lhs instanceof IdentifierAst id && Context.modules.containsKey(id.text)) {
var mod = Context.modules.get(id.text);

if (lhs instanceof CCOperationAst op) {
var mod = (PiccodeModule) op.execute(frame);
if (!(rhs instanceof CallAst) && !(rhs instanceof IdentifierAst)) {
throw new PiccodeException(file, line, column, "No node " + rhs + " found in module " + id.text);
throw new PiccodeException(file, line, column, "No node " + rhs + " found in module " + mod.name);
}


var id = new IdentifierAst(mod.name);
id.file = file;
id.line = line;
id.column = column;
return process(id, mod, frame);
}

if (lhs instanceof IdentifierAst id && Context.top.getValue(id.text) != null) {
var mod = Context.top.getValue(id.text);
if (!(rhs instanceof CallAst) && !(rhs instanceof IdentifierAst)) {
throw new PiccodeException(file, line, column, "No node " + rhs + " found in module " + id.text);
}
return process(id, (PiccodeModule)mod, frame);
}

var err = new PiccodeException(file, line, column, "Invalid use of `::`. Expected a module on the lhs");
var err = new PiccodeException(file, line, column, "Invalid use of `::`. Expected a module on the lhs, but found " + Chalk.on(lhs.toString()).red());
err.frame = frame;

if (lhs instanceof IdentifierAst id) {
var nm = Context.top.getSimilarName(id.text);
if (nm != null && !nm.isEmpty()) {
var note = new PiccodeSimpleNote("Did you mean `" + Chalk.on(nm).green() + "` instead of `" + Chalk.on(id.text).red() + "` ?");
err.addNote(note);
}
}
throw err;
}

Expand Down Expand Up @@ -75,8 +95,7 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame)
return result;
}
if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) {
node.execute(frame);
return Context.modules.get(_id.text);
return node.execute(frame);
}
}

Expand Down Expand Up @@ -105,7 +124,7 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame)
}
if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) {
node.execute(frame);
return Context.modules.get(_id.text);
return ctx.getValue(_id.text);
}
}

Expand Down
70 changes: 1 addition & 69 deletions src/main/java/org/piccode/ast/DotOperationAst.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public String toString() {
@Override
public PiccodeValue execute(Integer frame) {

if (lhs instanceof IdentifierAst id && Context.modules.containsKey(id.text)) {
if (lhs instanceof IdentifierAst id && Context.top.getValue(id.text) != null && Context.top.getValue(id.text) instanceof PiccodeModule) {
var err = new PiccodeException(file, line, column, "Cannot access the module `" + id.text + "` using dot. Please use `::` instead");
err.frame = frame;
throw err;
Expand All @@ -56,10 +56,6 @@ public PiccodeValue execute(Integer frame) {
return processArrayIndexing(tupl.array(), rhs.execute(frame), frame);
}

if (left instanceof PiccodeModule mod) {
return process(new IdentifierAst(mod.name), mod, frame);
}

if (!(left instanceof PiccodeObject)) {
var err = new PiccodeException(file, line, column, "Invalid expression on the side of `.` : " + lhs + " has value " + left + " which is not an object");
err.frame = frame;
Expand Down Expand Up @@ -112,70 +108,6 @@ public PiccodeValue execute(Integer frame) {
return value;
}

private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame) {
var ctx = frame == null
? Context.top
: Context.getContextAt(frame);

if (rhs instanceof IdentifierAst _id) {
for (var node : mod.nodes) {
if (node instanceof VarDecl vd && vd.name.equals(_id.text)) {
return node.execute(frame);
}
if (node instanceof FunctionAst func && func.name.equals(_id.text)) {
node.execute(frame);
var result = ctx.getValue(_id.text);
if (result == null) {
var err = new PiccodeException(func.file, func.line, func.column, "Function `" + _id.text + "` is not defined");
err.frame = frame;
var nm = ctx.getSimilarName(_id.text);
if (nm != null && !nm.isEmpty()) {
var note = new PiccodeException(func.file, func.line, func.column, "Maybe you meant `" + nm + "`");
err.addNote(note);
}
throw err;
}
return result;
}
if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) {
node.execute(frame);
return Context.modules.get(_id.text);
}
}

var err = new PiccodeException(file, line, column, "No function or identifier " + _id.text + " found in module " + id.text);
err.frame = frame;
throw err;
}

var call = (CallAst) rhs;

if (!(call.expr instanceof IdentifierAst)) {
var err = new PiccodeException(file, line, column, "Invalid function reference in module access module " + id.text + ": " + call.expr);
err.frame = frame;
throw err;
}

var _id = (IdentifierAst) call.expr;
for (var node : mod.nodes) {
if (node instanceof VarDecl vd && vd.name.equals(_id.text)) {
return node.execute(frame);
}
if (node instanceof FunctionAst func && func.name.equals(_id.text)) {
node.execute(frame);
//return Context.top.getValue(_id.text);
return call.execute(frame);
}
if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) {
node.execute(frame);
return Context.modules.get(_id.text);
}
}

var err = new PiccodeException(file, line, column, "No function or identifier " + _id.text + " found in module " + id.text);
err.frame = frame;
throw err;
}

private PiccodeValue processArrayIndexing(PiccodeValue[] arr, PiccodeValue execute, Integer frame) {
if (!(execute instanceof PiccodeNumber)) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/piccode/ast/FunctionAst.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ private String formatArgs() {

@Override
public PiccodeValue execute(Integer frame) {
var ctx = frame == null
? Context.top
: Context.getContextAt(frame);

Map<String, PiccodeValue> newArgs = new HashMap<>();
var cl = new PiccodeClosure(arg, newArgs, 0, body);
cl.creator = this;
Expand All @@ -62,7 +66,7 @@ public PiccodeValue execute(Integer frame) {
cl.file = file;
cl.column = column;
cl.line = line;
Context.addGlobal(name, cl);
ctx.putLocal(name, cl);
return cl;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/piccode/ast/IdentifierAst.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public PiccodeValue execute(Integer frame) {
var note = new PiccodeSimpleNote("Track size: " + ctx.getFramesCount());
err.addNote(note);

note = new PiccodeSimpleNote("Symbol table dump: " + sb.toString());
note = new PiccodeSimpleNote("Symbol table dump: \n" + sb.toString());
err.addNote(note);
throw err;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/piccode/ast/ImportAst.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private void executeLifted(List<Ast> moduleNodes, List<Ast> nestedLifted, Intege
}
}

private List<Ast> loadModuleFromStdLib(String module, Integer frame) {
public List<Ast> loadModuleFromStdLib(String module, Integer frame) {
var storage = getAppStorage();
var paths = List.of(storage, "./");
var nodes = new ArrayList<Ast>();
Expand Down
Loading
Loading