Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
arvyy committed Dec 13, 2024
1 parent 77cbe49 commit 991f5ab
Show file tree
Hide file tree
Showing 48 changed files with 371 additions and 162 deletions.
76 changes: 39 additions & 37 deletions language/src/main/java/com/github/arvyy/islisp/ISLISPContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleLanguage.Env;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.SourceSection;

import java.util.*;
import java.util.function.Function;
Expand Down Expand Up @@ -58,15 +59,15 @@ public ISLISPContext(ISLISPTruffleLanguage language, Env env) {
this.language = language;
this.env = env;
modules = new HashMap<>();
modules.put("ROOT", new ISLISPModule());
modules.put("builtin/truffle.lisp", new ISLISPModule());
modules.put("ROOT", new ISLISPModule("main"));
modules.put("builtin/truffle.lisp", new ISLISPModule("builtin/truffle.lisp"));
symbolProperties = new HashMap<>();
symbols = new HashMap<>();
currentOutputStream = new ValueReference();
currentOutputStream = new ValueReference(null);
currentOutputStream.setValue(new LispStream(null, env.out()));
currentInputStream = new ValueReference();
currentInputStream = new ValueReference(null);
currentInputStream.setValue(new LispStream(env.in(), null));
currentErrorStream = new ValueReference();
currentErrorStream = new ValueReference(null);
currentErrorStream.setValue(new LispStream(null, env.err()));
initBuiltinVars();
initBuiltinClasses();
Expand All @@ -93,11 +94,11 @@ public ISLISPModule getModule(String module) {
* @param requiredModules list of required modules, must exist.
* @param exports list of exports.
*/
public void createModule(String module, List<String> requiredModules, List<SymbolReference> exports) {
public void createModule(String module, List<String> requiredModules, List<Symbol> exports) {
if (modules.containsKey(module)) {
throw new ParsingException(null, "Module already defined: " + module);
}
var m = new ISLISPModule();
var m = new ISLISPModule(module);
m.addImport(modules.get("ROOT"));
for (var req: requiredModules) {
if (!modules.containsKey(req)) {
Expand Down Expand Up @@ -134,11 +135,11 @@ public LispFunction popHandler() {
}

void initGlobalFunction(String name, Function<TruffleLanguage<?>, LispFunction> f) {
modules.get("ROOT").registerFunction(namedSymbol(name).identityReference(), f.apply(language));
modules.get("ROOT").registerFunction(namedSymbol(name), f.apply(language));
}

void initTruffleLibFunction(String name, Function<TruffleLanguage<?>, LispFunction> f) {
modules.get("builtin/truffle.lisp").registerFunction(namedSymbol(name).identityReference(), f.apply(language));
modules.get("builtin/truffle.lisp").registerFunction(namedSymbol(name), f.apply(language));
}

/**
Expand Down Expand Up @@ -247,7 +248,7 @@ void initGlobalFunctions() {
}

private void initInitializeObjectMethod() {
var object = modules.get("ROOT").lookupClass(namedSymbol("<object>").identityReference());
var object = modules.get("ROOT").lookupClass(namedSymbol("<object>"));
var initializeObjectDescriptor = new GenericFunctionDescriptor(1, true);
initializeObjectDescriptor.addPrimaryMethod(
new LispClass[] {object},
Expand All @@ -261,14 +262,14 @@ private void initInitializeObjectMethod() {
null);
registerGenericFunction(
"ROOT",
namedSymbol("initialize-object").identityReference(),
namedSymbol("initialize-object"),
false,
new LispFunction(initializeObjectExecutionNode.getCallTarget()),
initializeObjectDescriptor);
}

private void initCreateMethod() {
var stdClass = modules.get("ROOT").lookupClass(namedSymbol("<standard-class>").identityReference());
var stdClass = modules.get("ROOT").lookupClass(namedSymbol("<standard-class>"));
var createDescriptor = new GenericFunctionDescriptor(1, true);
createDescriptor.addPrimaryMethod(
new LispClass[] {stdClass},
Expand All @@ -282,7 +283,7 @@ private void initCreateMethod() {
null);
registerGenericFunction(
"ROOT",
namedSymbol("create").identityReference(),
namedSymbol("create"),
false,
new LispFunction(createExecutionNode.getCallTarget()),
createDescriptor);
Expand Down Expand Up @@ -310,16 +311,16 @@ void initBasicSetfExpander(String setfForm, String expandedForm) {
lst.addAll(forms.subList(1, forms.size()));
return Utils.listToValue(lst);
};
modules.get("ROOT").registerSetfTransformer(namedSymbol(setfForm).identityReference(), transformer);
modules.get("ROOT").registerSetfTransformer(namedSymbol(setfForm), transformer);
}

void initBuiltin(String name, String... parents) {
var root = modules.get("ROOT");
var symbol = namedSymbol(name);
var parentClasses = Arrays.stream(parents)
.map(pname -> root.lookupClass(namedSymbol(pname).identityReference()))
.map(pname -> root.lookupClass(namedSymbol(pname)))
.toList();
root.registerClass(symbol.identityReference(), new BuiltinClass(parentClasses, symbol, false));
root.registerClass(symbol, new BuiltinClass(parentClasses, symbol, false));
}

/**
Expand Down Expand Up @@ -356,8 +357,8 @@ void initBuiltinClasses() {
* Initialize builtin constants.
*/
void initBuiltinVars() {
modules.get("ROOT").registerGlobalVar(getNil().identityReference(), getNil(), true);
modules.get("ROOT").registerGlobalVar(getT().identityReference(), getT(), true);
modules.get("ROOT").registerGlobalVar(getNil(), getNil(), true, null);
modules.get("ROOT").registerGlobalVar(getT(), getT(), true, null);
}

/**
Expand All @@ -367,10 +368,11 @@ void initBuiltinVars() {
* @param symbolReference variable name
* @param init initialization value
* @param readonly if the variable is a constant
* @param sourceSection source section corresponding to declaration site
*/
@CompilerDirectives.TruffleBoundary
public void registerGlobalVar(String module, SymbolReference symbolReference, Object init, boolean readonly) {
modules.get(module).registerGlobalVar(symbolReference, init, readonly);
public void registerGlobalVar(String module, Symbol symbolReference, Object init, boolean readonly, SourceSection sourceSection) {
modules.get(module).registerGlobalVar(symbolReference, init, readonly, sourceSection);
}

/**
Expand All @@ -384,7 +386,7 @@ public void registerGlobalVar(String module, SymbolReference symbolReference, Ob
public ValueReference lookupSymbolProperty(SymbolReference symbol, SymbolReference property) {
symbolProperties.putIfAbsent(symbol, new HashMap<>());
var properties = symbolProperties.get(symbol);
properties.putIfAbsent(property, new ValueReference());
properties.putIfAbsent(property, new ValueReference(null));
return properties.get(property);
}

Expand All @@ -396,7 +398,7 @@ public ValueReference lookupSymbolProperty(SymbolReference symbol, SymbolReferen
* @return variable's value reference or null if not found
*/
@CompilerDirectives.TruffleBoundary
public ValueReference lookupGlobalVar(String module, SymbolReference symbolReference) {
public ValueReference lookupGlobalVar(String module, Symbol symbolReference) {
return modules.get(module).lookupGlobalVar(symbolReference);
}

Expand All @@ -409,7 +411,7 @@ public ValueReference lookupGlobalVar(String module, SymbolReference symbolRefer
* @param transformer transformer.
*/
@CompilerDirectives.TruffleBoundary
public void registerSetfTransformer(String module, SymbolReference symbolReference, SetfTransformer transformer) {
public void registerSetfTransformer(String module, Symbol symbolReference, SetfTransformer transformer) {
modules.get(module).registerSetfTransformer(symbolReference, transformer);
}

Expand All @@ -421,7 +423,7 @@ public void registerSetfTransformer(String module, SymbolReference symbolReferen
* @return setf transformer or null if undefined
*/
@CompilerDirectives.TruffleBoundary
public SetfTransformer lookupSetfTransformer(String module, SymbolReference symbolReference) {
public SetfTransformer lookupSetfTransformer(String module, Symbol symbolReference) {
return modules.get(module).lookupSetfTransformer(symbolReference);
}

Expand All @@ -433,7 +435,7 @@ public SetfTransformer lookupSetfTransformer(String module, SymbolReference symb
* @param v value reference.
*/
@CompilerDirectives.TruffleBoundary
public void registerDynamicVar(String module, SymbolReference symbolReference, ValueReference v) {
public void registerDynamicVar(String module, Symbol symbolReference, ValueReference v) {
modules.get(module).registerDynamicVar(symbolReference, v);
}

Expand All @@ -445,7 +447,7 @@ public void registerDynamicVar(String module, SymbolReference symbolReference, V
* @return dynamic variable value reference
*/
@CompilerDirectives.TruffleBoundary
public ValueReference lookupDynamicVar(String module, SymbolReference symbolReference) {
public ValueReference lookupDynamicVar(String module, Symbol symbolReference) {
return modules.get(module).lookupDynamicVar(symbolReference);
}

Expand All @@ -457,7 +459,7 @@ public ValueReference lookupDynamicVar(String module, SymbolReference symbolRefe
* @param function function value
*/
@CompilerDirectives.TruffleBoundary
public void registerFunction(String module, SymbolReference symbolReference, LispFunction function) {
public void registerFunction(String module, Symbol symbolReference, LispFunction function) {
modules.get(module).registerFunction(symbolReference, function);
}

Expand All @@ -469,7 +471,7 @@ public void registerFunction(String module, SymbolReference symbolReference, Lis
* @return function or null if undefined
*/
@CompilerDirectives.TruffleBoundary
public LispFunction lookupFunction(String module, SymbolReference symbolReference) {
public LispFunction lookupFunction(String module, Symbol symbolReference) {
return lookupFunction(module, symbolReference, false);
}

Expand All @@ -482,7 +484,7 @@ public LispFunction lookupFunction(String module, SymbolReference symbolReferenc
* @return function or null if undefined
*/
@CompilerDirectives.TruffleBoundary
public LispFunction lookupFunction(String module, SymbolReference symbolReference, boolean setf) {
public LispFunction lookupFunction(String module, Symbol symbolReference, boolean setf) {
return modules.get(module).lookupFunction(symbolReference, setf);
}

Expand All @@ -498,7 +500,7 @@ public LispFunction lookupFunction(String module, SymbolReference symbolReferenc
@CompilerDirectives.TruffleBoundary
public void registerGenericFunction(
String module,
SymbolReference symbolReference,
Symbol symbolReference,
boolean setf,
LispFunction function,
GenericFunctionDescriptor descriptor
Expand All @@ -517,7 +519,7 @@ public void registerGenericFunction(
@CompilerDirectives.TruffleBoundary
public GenericFunctionDescriptor lookupGenericFunctionDispatchTree(
String module,
SymbolReference symbolReference,
Symbol symbolReference,
boolean setf
) {
return modules.get(module).lookupGenericFunctionDispatchTree(symbolReference, setf);
Expand All @@ -531,7 +533,7 @@ public GenericFunctionDescriptor lookupGenericFunctionDispatchTree(
* @param function macro implementation function
*/
@CompilerDirectives.TruffleBoundary
public void registerMacro(String module, SymbolReference symbolReference, LispFunction function) {
public void registerMacro(String module, Symbol symbolReference, LispFunction function) {
modules.get(module).registerMacro(symbolReference, function);
}

Expand All @@ -543,7 +545,7 @@ public void registerMacro(String module, SymbolReference symbolReference, LispFu
* @return macro function or null if undefined
*/
@CompilerDirectives.TruffleBoundary
public LispFunction lookupMacro(String module, SymbolReference symbolReference) {
public LispFunction lookupMacro(String module, Symbol symbolReference) {
return modules.get(module).lookupMacro(symbolReference);
}

Expand All @@ -555,7 +557,7 @@ public LispFunction lookupMacro(String module, SymbolReference symbolReference)
* @param clazz class instance
*/
@CompilerDirectives.TruffleBoundary
public void registerClass(String module, SymbolReference symbolReference, LispClass clazz) {
public void registerClass(String module, Symbol symbolReference, LispClass clazz) {
modules.get(module).registerClass(symbolReference, clazz);
}

Expand All @@ -567,7 +569,7 @@ public void registerClass(String module, SymbolReference symbolReference, LispCl
* @return class or null if doesn't exist
*/
@CompilerDirectives.TruffleBoundary
public LispClass lookupClass(String module, SymbolReference symbolReference) {
public LispClass lookupClass(String module, Symbol symbolReference) {
return modules.get(module).lookupClass(symbolReference);
}

Expand All @@ -580,7 +582,7 @@ public LispClass lookupClass(String module, SymbolReference symbolReference) {
*/
@CompilerDirectives.TruffleBoundary
public LispClass lookupClass(String module, String name) {
return lookupClass(module, namedSymbol(name).identityReference());
return lookupClass(module, namedSymbol(name));
}

/**
Expand All @@ -600,7 +602,7 @@ public LispClass lookupClass(String name) {
* @param ref class name's symbol reference
* @return class instance or null
*/
public LispClass lookupClass(SymbolReference ref) {
public LispClass lookupClass(Symbol ref) {
return lookupClass("ROOT", ref);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ protected Object getLanguageView(ISLISPContext context, Object value) {
return new ISLISPTruffleLanguageView(context, value);
}

@Override
protected Object getScope(ISLISPContext context) {
return context.getModule("ROOT");
}

@Override
protected OptionDescriptors getOptionDescriptors() {
return new ISLISPTruffleLanguageOptionDescriptors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ DirectCallNode getCreateVectorNode() {
var ctx = ISLISPContext.get(this);
createVectorNode = this.insert(
DirectCallNode.create(
ctx.lookupFunction("ROOT", ctx.namedSymbol("create-vector").identityReference())
ctx.lookupFunction("ROOT", ctx.namedSymbol("create-vector"))
.callTarget()));
}
return createVectorNode;
Expand All @@ -52,7 +52,7 @@ DirectCallNode getLengthNode() {
CompilerDirectives.transferToInterpreterAndInvalidate();
lengthNode = this.insert(
DirectCallNode.create(
ctx.lookupFunction("ROOT", ctx.namedSymbol("length").identityReference())
ctx.lookupFunction("ROOT", ctx.namedSymbol("length"))
.callTarget()));
}
return lengthNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Object execute(VirtualFrame frame) {
if (initializeObjectFunction == null) {
var ctx = ISLISPContext.get(this);
initializeObjectFunction = ctx.lookupFunction(
"ROOT", ctx.namedSymbol("initialize-object").identityReference());
"ROOT", ctx.namedSymbol("initialize-object"));
}
var clazz = (StandardClass) frame.getArguments()[1];
var obj = new StandardClassObject(clazz, clazz.shape().getFactory().create());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public class ISLISPDefaultHandler extends RootNode {
public Object execute(VirtualFrame frame) {
var ctx = ISLISPContext.get(this);
var exitFunction = ctx.lookupFunction(
"ROOT", ctx.namedSymbol("exit").identityReference());
"ROOT", ctx.namedSymbol("exit"));
var reportConditionFunction = ctx.lookupFunction(
"ROOT", ctx.namedSymbol("report-condition").identityReference());
"ROOT", ctx.namedSymbol("report-condition"));
var errorOutputFunction = ctx.lookupFunction(
"ROOT", ctx.namedSymbol("error-output").identityReference());
"ROOT", ctx.namedSymbol("error-output"));
var errorOutput = dispatchNode.executeDispatch(errorOutputFunction, new Object[]{});
var condition = frame.getArguments()[1];
dispatchNode.executeDispatch(reportConditionFunction, new Object[]{condition, errorOutput});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ LispClass cList() {
if (cList == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
var ctx = ISLISPContext.get(this);
cList = ctx.lookupClass("ROOT", ctx.namedSymbol("<list>").identityReference());
cList = ctx.lookupClass("ROOT", ctx.namedSymbol("<list>"));
}
return cList;
}
Expand All @@ -62,7 +62,7 @@ LispClass cInteger() {
if (cInteger == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
var ctx = ISLISPContext.get(this);
cInteger = ctx.lookupClass("ROOT", ctx.namedSymbol("<integer>").identityReference());
cInteger = ctx.lookupClass("ROOT", ctx.namedSymbol("<integer>"));
}
return cInteger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final Object execute(VirtualFrame frame) {
Object isEqual(Object o1, Object o2) {
var nil = ISLISPContext.get(this).getNil();
var isEq = getEqCallNode().call(null, o1, o2);
if (isEq instanceof Symbol s && s.identityReference() == nil.identityReference()) {
if (isEq instanceof Symbol s && s == nil) {
return executeGeneric(o1, o2);
} else {
return isEq;
Expand Down Expand Up @@ -153,7 +153,7 @@ DirectCallNode getEqCallNode() {
CompilerDirectives.transferToInterpreterAndInvalidate();
var ctx = ISLISPContext.get(this);
var callNode = DirectCallNode.create(
ctx.lookupFunction("ROOT", ctx.namedSymbol("eq").identityReference())
ctx.lookupFunction("ROOT", ctx.namedSymbol("eq"))
.callTarget());
eqCallNode = insert(callNode);
}
Expand Down
Loading

0 comments on commit 991f5ab

Please sign in to comment.