Skip to content

Commit

Permalink
Add computer/2, fix error propagation so it can be analyzed.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlchemicRaker committed Dec 6, 2019
1 parent 68bf6aa commit 9b7efb1
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ apply plugin: 'net.minecraftforge.gradle.forge'
//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.


version = "0.3.6"
version = "0.3.7"
group = "com.syntheticentropy.ocpro" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "ocpro"

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/syntheticentropy/ocpro/OpenProlog.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class OpenProlog
@SuppressWarnings("WeakerAccess")
public static final String NAME = "OpenProlog";
@SuppressWarnings("WeakerAccess")
public static final String VERSION = "0.3.6";
public static final String VERSION = "0.3.7";

// resource path
static final String RESOURCE_PATH = "/com/syntheticentropy/ocpro/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ public long waitingCall(int ticks) {

public void crash(String e) {
forceExecutionResult.add(new ExecutionResult.Error(e));

// machine.crash(e);
if (machine.isRunning()) {
machine.crash(e);
close();
}
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/syntheticentropy/ocpro/PrologVM.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public void run() {
} else {
// run main query
}
} catch (JIPRuntimeException e) {
state = State.Terminated;
owner.crash(e.getMessage());
} catch (Exception e) {
state = State.Terminated;
owner.crash(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public boolean unify(Hashtable<Variable, Variable> varsTbl) {

if (attempt) {
// One result from this function = one call
getJIPEngine().getOwner().synchronizedCall(() -> null);
// getJIPEngine().getOwner().synchronizedCall(() -> null);

return (addressAtom == null || addressAtom.unify(addressParam, varsTbl)) &&
(methodAtom == null || methodAtom.unify(methodParam, varsTbl));
}
}

// One result from this function = one call
getJIPEngine().getOwner().synchronizedCall(() -> null);
// getJIPEngine().getOwner().synchronizedCall(() -> null);
return false;
}

Expand Down
73 changes: 73 additions & 0 deletions src/main/java/com/syntheticentropy/ocpro/builtin/Computer2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.syntheticentropy.ocpro.builtin;

import com.syntheticentropy.ocpro.PrologArchitecture;
import com.ugos.jiprolog.engine.Atom;
import com.ugos.jiprolog.engine.JIPTypeException;
import com.ugos.jiprolog.engine.PrologObject;
import com.ugos.jiprolog.engine.Variable;
import li.cil.oc.api.network.Connector;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Function;

public class Computer2 extends OcproBuiltIn {
private Map<String, Function<PrologArchitecture, Object>> exposedValues = createMap();

private static Map<String, Function<PrologArchitecture, Object>> createMap() {
Map<String, Function<PrologArchitecture, Object>> m = new HashMap<>();
m.put("realTime", o -> System.currentTimeMillis() / 1000.0);
m.put("uptime", o -> o.machine.upTime());
m.put("address", o -> o.machine.node().address());
// m.put("freeMemory", o -> o.)
// m.put("totalMemory", o -> o.)
m.put("energy", o -> ((Connector) o.machine.node()).globalBuffer());
m.put("maxEnergy", o -> li.cil.oc.Settings.get().ignorePower() ?
Double.POSITIVE_INFINITY :
((Connector) o.machine.node()).globalBufferSize());

return m;
}

private Iterator<Map.Entry<String, Function<PrologArchitecture, Object>>> resultIterator = null;

@Override
public boolean unify(Hashtable<Variable, Variable> varsTbl) {
final PrologObject keyParam = getParam(1);
final PrologObject valueParam = getParam(2);

if (resultIterator == null) {
final PrologObject keyTerm = getRealTerm(keyParam);

// we know that a provided key must be an atom
if (!(keyTerm == null || keyTerm instanceof Atom))
throw new JIPTypeException(JIPTypeException.ATOM, keyTerm);

resultIterator = exposedValues.entrySet().iterator();
}

PrologArchitecture owner = m_jipEngine.getOwner();

while (resultIterator.hasNext()) {
Map.Entry<String, Function<PrologArchitecture, Object>> next = resultIterator.next();
PrologObject keyAtom = Atom.createAtom(next.getKey());
if (!(keyAtom == null || keyAtom.unifiable(keyParam))) continue;

// don't evaluate the value until after we've matched a key
PrologObject value = rawToPrologObject(next.getValue().apply(owner));
if (!(value == null || value.unifiable(valueParam))) continue;

return (keyAtom == null || keyAtom.unify(keyParam, varsTbl)) &&
(value == null || value.unify(valueParam, varsTbl));
}

return false;
}

@Override
public boolean hasMoreChoicePoints() {
return resultIterator == null || resultIterator.hasNext();
}
}
1 change: 1 addition & 0 deletions src/main/java/com/ugos/jiprolog/engine/BuiltInFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ final class BuiltInFactory extends Object {
m_builtInTable.put("consult/2", new Consult2());
m_builtInTable.put("pop_signal/1", new PopSignal1());
m_builtInTable.put("term_string/2", new TermString2());
m_builtInTable.put("computer/2", new Computer2());

// catch((X = 1, throw(gulp)), E, (write(ok-X), nl)).
//m_BuiltInTable.put("wait/1", new Wait1());
Expand Down

0 comments on commit 9b7efb1

Please sign in to comment.