Skip to content

Commit

Permalink
Add stackindex keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdodd4598 committed Dec 11, 2023
1 parent b3f1a1c commit 02a1415
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 97 deletions.
30 changes: 29 additions & 1 deletion run/classes.dssl
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,60 @@ item .printRecursive
# ------------------- #

/Person {
/__init__ {
"Constructing Person!" println
Person .supers { .__init__ } foreach
} macro

/talk {
type " talked." ~ println
} macro
} class

/Worker Person {
/__init__ {
"Constructing Worker!" println
Worker .supers { .__init__ } foreach
} macro

/work {
type " worked." ~ println
} macro
} class

/Miner Worker {
/__init__ {
"Constructing Miner!" println
Miner .supers { .__init__ } foreach
} macro

/mine {
type " mined." ~ println
} macro
} class

/Programmer Worker {
/__init__ {
"Constructing Programmer!" println
Programmer .supers { .__init__ } foreach
} macro

/code {
type " coded." ~ println
} macro
} class

/Satoshi Miner Programmer { } class
/Satoshi Programmer Miner {
/__init__ {
"Constructing Satoshi!" println
Satoshi .supers { .__init__ } foreach
} macro
} class

/satoshi Satoshi new def

'\n' print

satoshi .talk
satoshi .work
satoshi .mine
Expand Down
1 change: 1 addition & 0 deletions src/dssl.sable
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Tokens
dup = 'dup';

stacksize = 'stacksize';
stackindex = 'stackindex';

read = 'read';
print = 'print';
Expand Down
1 change: 1 addition & 0 deletions src/dssl/analysis/Analysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public interface Analysis extends Switch
void caseTPop(TPop node);
void caseTDup(TDup node);
void caseTStacksize(TStacksize node);
void caseTStackindex(TStackindex node);
void caseTRead(TRead node);
void caseTPrint(TPrint node);
void caseTPrintln(TPrintln node);
Expand Down
6 changes: 6 additions & 0 deletions src/dssl/analysis/AnalysisAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ public void caseTStacksize(TStacksize node)
defaultCase(node);
}

@Override
public void caseTStackindex(TStackindex node)
{
defaultCase(node);
}

@Override
public void caseTRead(TRead node)
{
Expand Down
2 changes: 1 addition & 1 deletion src/dssl/interpret/BuiltIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class BuiltIn {
public static final Map<@NonNull String, Clazz> CLAZZ_MAP = new HashMap<>();
public static final Map<@NonNull String, Clazz> MODULE_MAP = new HashMap<>();

public static final @NonNull Clazz OBJECT_CLAZZ = clazz(Clazz.objectClazz());
public static final @NonNull Clazz OBJECT_CLAZZ = clazz(new Clazz(null, OBJECT, ClazzType.INTERNAL, null, null));

public static final @NonNull Clazz CLASS_CLAZZ = clazz(new Clazz(CLASS, ClazzType.INTERNAL));
public static final @NonNull Clazz LABEL_CLAZZ = clazz(new Clazz(LABEL, ClazzType.INTERNAL));
Expand Down
24 changes: 10 additions & 14 deletions src/dssl/interpret/Clazz.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,30 @@ public Clazz(@NonNull String identifier, @NonNull ClazzType type, Clazz... super
this(null, identifier, type, null, new ArrayList<>(Arrays.asList(supers)));
}

public Clazz(String prev, @NonNull String extension, @NonNull ClazzType type, HierarchicalScope base, ArrayList<Clazz> supers) {
public Clazz(String prev, @NonNull String extension, @NonNull ClazzType type, @Nullable HierarchicalScope base, @Nullable ArrayList<Clazz> supers) {
fullIdentifier = Helpers.extendedIdentifier(prev, extension);
shallowIdentifier = extension;
this.type = type;

if (!objectClazzInit) {
supers.add(BuiltIn.OBJECT_CLAZZ);
if (supers == null) {
this.supers = new ArrayList<>();
}
else {
if (supers.isEmpty()) {
supers.add(BuiltIn.OBJECT_CLAZZ);
}
this.supers = supers.stream().distinct().collect(Collectors.toList());
}
this.supers = supers.stream().distinct().collect(Collectors.toList());

defHierarchy = getHierarchy(base, HierarchicalScope::getDefHierarchy);
macroHierarchy = getHierarchy(base, HierarchicalScope::getMacroHierarchy);
clazzHierarchy = getHierarchy(base, HierarchicalScope::getClazzHierarchy);
}

protected <K, V> Hierarchy<K, V> getHierarchy(HierarchicalScope base, Function<HierarchicalScope, Hierarchy<K, V>> function) {
protected <K, V> Hierarchy<K, V> getHierarchy(@Nullable HierarchicalScope base, Function<HierarchicalScope, Hierarchy<K, V>> function) {
return (base == null ? new Hierarchy<K, V>() : function.apply(base)).branch(Helpers.map(supers, function));
}

private static boolean objectClazzInit = false;

public static @NonNull Clazz objectClazz() {
objectClazzInit = true;
@NonNull Clazz clazz = new Clazz(BuiltIn.OBJECT, ClazzType.INTERNAL);
objectClazzInit = false;
return clazz;
}

@SuppressWarnings("null")
public @NonNull Element clazzElement() {
if (elem == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/dssl/interpret/HierarchicalScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.*;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.*;

import dssl.Hierarchy;
import dssl.interpret.element.*;
Expand Down Expand Up @@ -78,7 +78,7 @@ public default Clazz setClazz(@NonNull String shallowIdentifier, @NonNull Clazz
}

@Override
public default Clazz setClazz(@NonNull String shallowIdentifier, @NonNull ClazzType type, HierarchicalScope base, ArrayList<Clazz> supers) {
public default Clazz setClazz(@NonNull String shallowIdentifier, @NonNull ClazzType type, @Nullable HierarchicalScope base, @NonNull ArrayList<Clazz> supers) {
return setClazz(shallowIdentifier, new Clazz(scopeIdentifier(), shallowIdentifier, type, base, supers), true);
}

Expand Down
2 changes: 1 addition & 1 deletion src/dssl/interpret/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ else if (hasClazz(identifier, true)) {

public Clazz getClazz(@NonNull String shallowIdentifier);

public Clazz setClazz(@NonNull String shallowIdentifier, @NonNull ClazzType type, HierarchicalScope base, ArrayList<Clazz> supers);
public Clazz setClazz(@NonNull String shallowIdentifier, @NonNull ClazzType type, @Nullable HierarchicalScope base, @NonNull ArrayList<Clazz> supers);

public Clazz removeClazz(@NonNull String shallowIdentifier);

Expand Down
17 changes: 17 additions & 0 deletions src/dssl/interpret/TokenExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ protected <T extends Token> TokenFunction get(Class<T> clazz) {
TOKEN_FUNCTION_MAP.put(TDup.class, TokenExecutor::onDup);

TOKEN_FUNCTION_MAP.put(TStacksize.class, TokenExecutor::onStacksize);
TOKEN_FUNCTION_MAP.put(TStackindex.class, TokenExecutor::onStackindex);

TOKEN_FUNCTION_MAP.put(TRead.class, TokenExecutor::onRead);
TOKEN_FUNCTION_MAP.put(TPrint.class, TokenExecutor::onPrint);
Expand Down Expand Up @@ -582,6 +583,22 @@ protected <T extends Token> TokenFunction get(Class<T> clazz) {
return TokenResult.PASS;
}

protected @NonNull TokenResult onStackindex(@NonNull Token token) {
@NonNull Element elem = pop();
IntElement intElem = elem.asInt(this);
if (intElem == null) {
throw new IllegalArgumentException(String.format("Keyword \"stackindex\" requires %s element as argument!", Helpers.NON_NEGATIVE_INT));
}

int primitiveInt = intElem.primitiveInt();
if (primitiveInt < 0) {
throw new IllegalArgumentException(String.format("Keyword \"stackindex\" requires %s element as argument!", Helpers.NON_NEGATIVE_INT));
}

push(peekAt(primitiveInt));
return TokenResult.PASS;
}

protected @NonNull TokenResult onRead(@NonNull Token token) {
String str = interpreter.hooks.read();
push(str == null ? NullElement.INSTANCE : new StringElement(str));
Expand Down
2 changes: 1 addition & 1 deletion src/dssl/interpret/element/LabelElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Clazz getClazz() {
return scope.getClazz(shallowIdentifier);
}

public void setClazz(@NonNull ClazzType type, HierarchicalScope base, ArrayList<Clazz> supers) {
public void setClazz(@NonNull ClazzType type, @Nullable HierarchicalScope base, @NonNull ArrayList<Clazz> supers) {
Clazz prev = scope.setClazz(shallowIdentifier, type, base, supers);
if (prev != null && !scope.canShadow()) {
throw shadowError("class");
Expand Down
2 changes: 1 addition & 1 deletion src/dssl/interpret/element/clazz/InstanceElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public Clazz getClazz(@NonNull String shallowIdentifier) {
}

@Override
public Clazz setClazz(@NonNull String shallowIdentifier, @NonNull ClazzType type, HierarchicalScope base, ArrayList<Clazz> supers) {
public Clazz setClazz(@NonNull String shallowIdentifier, @NonNull ClazzType type, @Nullable HierarchicalScope base, @NonNull ArrayList<Clazz> supers) {
checkCollision(shallowIdentifier);
return clazzMap.put(shallowIdentifier, new Clazz(scopeIdentifier, shallowIdentifier, type, base, supers));
}
Expand Down
Loading

0 comments on commit 02a1415

Please sign in to comment.