Skip to content

Commit

Permalink
Merge PR #25: Add primitives for SomSom
Browse files Browse the repository at this point in the history
  • Loading branch information
smarr authored Apr 9, 2021
2 parents 9f45412 + ef2c4a9 commit ca93052
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/som/compiler/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ private boolean expect(final Symbol s) {
if (accept(s)) {
return true;
}
StringBuffer err = new StringBuffer("Error: " + filename + ":" +
StringBuilder err = new StringBuilder("Error: " + filename + ":" +
lexer.getCurrentLineNumber() +
": unexpected symbol, expected: " + s.toString()
+ ", but found: " + sym.toString());
Expand All @@ -295,7 +295,7 @@ private boolean expectOneOf(final List<Symbol> ss) {
if (acceptOneOf(ss)) {
return true;
}
StringBuffer err = new StringBuffer("Error: " + filename + ":" +
StringBuilder err = new StringBuilder("Error: " + filename + ":" +
lexer.getCurrentLineNumber() + ": unexpected symbol, expected one of: ");
for (Symbol s : ss) {
err.append(s.toString() + ", ");
Expand Down Expand Up @@ -357,7 +357,7 @@ private void binaryPattern(final MethodGenerationContext mgenc) {
}

private void keywordPattern(final MethodGenerationContext mgenc) {
StringBuffer kw = new StringBuffer();
StringBuilder kw = new StringBuilder();
do {
kw.append(keyword());
mgenc.addArgumentIfAbsent(argument());
Expand Down Expand Up @@ -523,11 +523,7 @@ private void assignments(final MethodGenerationContext mgenc, final List<String>

private String assignment(final MethodGenerationContext mgenc) throws ParseError {
String v = variable();
SSymbol var = universe.symbolFor(v);
mgenc.addLiteralIfAbsent(var, this);

expect(Assign);

return v;
}

Expand Down Expand Up @@ -647,7 +643,7 @@ private boolean binaryOperand(final MethodGenerationContext mgenc)

private void keywordMessage(final MethodGenerationContext mgenc,
final boolean superSend) throws ProgramDefinitionError {
StringBuffer kw = new StringBuffer();
StringBuilder kw = new StringBuilder();
do {
kw.append(keyword());
formula(mgenc);
Expand Down Expand Up @@ -747,11 +743,11 @@ private SAbstractObject literalInteger(final boolean isNegative) {
expect(Integer);
return universe.newBigInteger(big);
} catch (NumberFormatException e) {
StringBuffer err = new StringBuffer("Error: " + filename + ":" +
String err = "Error: " + filename + ":" +
lexer.getCurrentLineNumber() +
": parsing number literal failed: '" + text.toString()
+ "'");
throw new IllegalStateException(err.toString());
+ "'";
throw new IllegalStateException(err);
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/som/primitives/DoublePrimitives.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import som.vmobjects.SDouble;
import som.vmobjects.SNumber;
import som.vmobjects.SPrimitive;
import som.vmobjects.SString;


public class DoublePrimitives extends Primitives {
Expand Down Expand Up @@ -162,5 +163,23 @@ public void invoke(final Frame frame, final Interpreter interpreter) {
frame.push(universe.newDouble(Double.POSITIVE_INFINITY));
}
});

installClassPrimitive(new SPrimitive("fromString:", universe) {
@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
SString arg = (SString) frame.pop();
frame.pop();

double d;

try {
d = Double.parseDouble(arg.getEmbeddedString());
} catch (NumberFormatException e) {
d = Double.NaN;
}

frame.push(universe.newDouble(d));
}
});
}
}
8 changes: 8 additions & 0 deletions src/som/primitives/IntegerPrimitives.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public void invoke(final Frame frame, final Interpreter interpreter) {
}
});

installInstancePrimitive(new SPrimitive("asDouble", universe) {
@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
SNumber self = (SNumber) frame.pop();
frame.push(self.primAsDouble(universe));
}
});

installInstancePrimitive(new SPrimitive("sqrt", universe) {
@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
Expand Down
59 changes: 59 additions & 0 deletions src/som/primitives/SystemPrimitives.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

package som.primitives;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import som.compiler.ProgramDefinitionError;
import som.interpreter.Frame;
import som.interpreter.Interpreter;
Expand All @@ -42,9 +47,11 @@ public SystemPrimitives(final Universe universe) {
super(universe);
}

@Override
public void installPrimitives() {
installInstancePrimitive(new SPrimitive("load:", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
SSymbol argument = (SSymbol) frame.pop();
frame.pop(); // not required
Expand All @@ -60,6 +67,7 @@ public void invoke(final Frame frame, final Interpreter interpreter) {

installInstancePrimitive(new SPrimitive("exit:", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
SInteger error = (SInteger) frame.pop();
universe.exit(error.getEmbeddedInteger());
Expand All @@ -68,6 +76,7 @@ public void invoke(final Frame frame, final Interpreter interpreter) {

installInstancePrimitive(new SPrimitive("global:", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
SSymbol argument = (SSymbol) frame.pop();
frame.pop(); // not required
Expand All @@ -78,6 +87,7 @@ public void invoke(final Frame frame, final Interpreter interpreter) {

installInstancePrimitive(new SPrimitive("global:put:", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
SAbstractObject value = frame.pop();
SSymbol argument = (SSymbol) frame.pop();
Expand All @@ -87,6 +97,7 @@ public void invoke(final Frame frame, final Interpreter interpreter) {

installInstancePrimitive(new SPrimitive("printString:", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
SString argument = (SString) frame.pop();
Universe.print(argument.getEmbeddedString());
Expand All @@ -95,15 +106,35 @@ public void invoke(final Frame frame, final Interpreter interpreter) {

installInstancePrimitive(new SPrimitive("printNewline", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
Universe.println("");
}
});

installInstancePrimitive(new SPrimitive("errorPrint:", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
SString argument = (SString) frame.pop();
Universe.errorPrint(argument.getEmbeddedString());
}
});

installInstancePrimitive(new SPrimitive("errorPrintln:", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
SString argument = (SString) frame.pop();
Universe.errorPrintln(argument.getEmbeddedString());
}
});

startMicroTime = System.nanoTime() / 1000L;
startTime = startMicroTime / 1000L;
installInstancePrimitive(new SPrimitive("time", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
frame.pop(); // ignore
int time = (int) (System.currentTimeMillis() - startTime);
Expand All @@ -113,6 +144,7 @@ public void invoke(final Frame frame, final Interpreter interpreter) {

installInstancePrimitive(new SPrimitive("ticks", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
frame.pop(); // ignore
int time = (int) (System.nanoTime() / 1000L - startMicroTime);
Expand All @@ -122,13 +154,40 @@ public void invoke(final Frame frame, final Interpreter interpreter) {

installInstancePrimitive(new SPrimitive("fullGC", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
frame.pop();
System.gc();
frame.push(universe.trueObject);
}
});

installInstancePrimitive(new SPrimitive("loadFile:", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
SString fileName = (SString) frame.pop();
frame.pop();

Path p = Paths.get(fileName.getEmbeddedString());
try {
String content = new String(Files.readAllBytes(p));
frame.push(universe.newString(content));
} catch (IOException e) {
frame.push(universe.nilObject);
}
}
});

installInstancePrimitive(new SPrimitive("printStackTrace", universe) {

@Override
public void invoke(final Frame frame, final Interpreter interpreter) {
frame.pop();
frame.printStackTrace();
frame.push(universe.trueObject);
}
});
}

private long startTime;
Expand Down
5 changes: 5 additions & 0 deletions src/som/vmobjects/SBigInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public SString primAsString(final Universe universe) {
return universe.newString(embeddedBiginteger.toString());
}

@Override
public SNumber primAsDouble(final Universe universe) {
return universe.newDouble(embeddedBiginteger.doubleValue());
}

private SNumber asSNumber(final BigInteger result, final Universe universe) {
if (result.bitLength() >= Long.SIZE) {
return universe.newBigInteger(result);
Expand Down
5 changes: 5 additions & 0 deletions src/som/vmobjects/SDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public SString primAsString(final Universe universe) {
return universe.newString(Double.toString(embeddedDouble));
}

@Override
public SDouble primAsDouble(final Universe universe) {
return this;
}

public SInteger primAsInteger(final Universe universe) {
return universe.newInteger((long) embeddedDouble);
}
Expand Down
5 changes: 5 additions & 0 deletions src/som/vmobjects/SInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public SString primAsString(final Universe universe) {
return universe.newString(Long.toString(embeddedInteger));
}

@Override
public SNumber primAsDouble(final Universe universe) {
return universe.newDouble(embeddedInteger);
}

@Override
public SNumber primSqrt(final Universe universe) {
double result = Math.sqrt(embeddedInteger);
Expand Down
2 changes: 2 additions & 0 deletions src/som/vmobjects/SNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
public abstract class SNumber extends SAbstractObject {
public abstract SString primAsString(Universe universe);

public abstract SNumber primAsDouble(Universe universe);

public abstract SNumber primSqrt(Universe universe);

public abstract SNumber primAdd(SNumber right, Universe universe);
Expand Down

0 comments on commit ca93052

Please sign in to comment.