Skip to content

Commit

Permalink
Merge branch 'feature-association'
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorzmazur committed Oct 3, 2015
2 parents 2a6a8fa + 6d364ca commit 8185ce4
Show file tree
Hide file tree
Showing 15 changed files with 625 additions and 15 deletions.
1 change: 1 addition & 0 deletions CMakeLists-jyacas.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set (JYACAS_SRCS ${CMAKE_CURRENT_BINARY_DIR}/CVersion.java
JavaYacas/net/sf/yacas/GenericClass.java
JavaYacas/net/sf/yacas/LispGenericClass.java
JavaYacas/net/sf/yacas/ArrayClass.java
JavaYacas/net/sf/yacas/AssociationClass.java
JavaYacas/net/sf/yacas/LispInFixOperator.java
JavaYacas/net/sf/yacas/LispPtr.java
JavaYacas/net/sf/yacas/LispObject.java
Expand Down
50 changes: 49 additions & 1 deletion CMakeLists-tests.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
include (CTest)
set(CTEST_PROJECT_NAME "yacas")

set (YACAS_TESTS openmath.yts macro.yts arithmetic.yts c_tex_form.yts calculus.yts canprove.yts comments.yts complex.yts deriv.yts dimensions.yts dot.yts journal.yts integrate.yts lists.yts logic_simplify_test.yts matrixpower.yts nthroot.yts outer.yts predicates.yts ode.yts tensors.yts trace.yts tr.yts multivar.yts numbers.yts io.yts programming.yts simplify.yts solve.yts sums.yts transforms.yts radsimp.yts linalg.yts orthopoly.yts poly.yts numerics.yts scopestack.yts plots.yts GaussianIntegers.yts nummethods.yts sturm.yts cyclotomic.yts binaryfactors.yts padic.yts calendar.yts newly.yts regress.yts)
set (YACAS_TESTS
arithmetic.yts
association.yts
binaryfactors.yts
c_tex_form.yts
calculus.yts
calendar.yts
canprove.yts
comments.yts
complex.yts
cyclotomic.yts
deriv.yts
dimensions.yts
dot.yts
GaussianIntegers.yts
integrate.yts
io.yts
journal.yts
linalg.yts
lists.yts
logic_simplify_test.yts
macro.yts
matrixpower.yts
multivar.yts
newly.yts
nthroot.yts
numbers.yts
numerics.yts
nummethods.yts
ode.yts
openmath.yts
orthopoly.yts
outer.yts
padic.yts
plots.yts
poly.yts
predicates.yts
programming.yts
radsimp.yts
regress.yts
scopestack.yts
simplify.yts
solve.yts
sturm.yts
sums.yts
tensors.yts
tr.yts
trace.yts
transforms.yts)

foreach (_test ${YACAS_TESTS})
add_test(NAME cyacas-${_test} WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND ${PROJECT_SOURCE_DIR}/tests/test-yacas "$<TARGET_FILE:yacas> -pc --rootdir ${PROJECT_SOURCE_DIR}/scripts:$<TARGET_FILE_DIR:yacas>/scripts" ${PROJECT_SOURCE_DIR}/tests ${_test})
Expand Down
67 changes: 67 additions & 0 deletions JavaYacas/net/sf/yacas/AssociationClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package net.sf.yacas;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

class AssociationClass extends GenericClass {

private static class Cmp implements Comparator<LispPtr> {
public Cmp(LispEnvironment env) {
this._env = env;
}

@Override
public int compare(LispPtr e1, LispPtr e2) {
try {
if (LispStandard.InternalStrictTotalOrder(_env, e1, e2))
return -1;

if (LispStandard.InternalEquals(_env, e1, e2))
return 0;

} catch (Exception e) {
// this shouldn't happen, right?
System.exit(255);
}

return 1;

}

LispEnvironment _env;
}

public AssociationClass(LispEnvironment env) {
this._map = new TreeMap<>(new Cmp(env));
}

@Override
public String Send(LispArgList aArgList) {
return null;
}

@Override
public String TypeName() {
return "\"Association\"";
}

public int Size() {
return _map.size();
}

public LispObject GetElement(LispObject k) throws Exception {
LispPtr v = _map.get(new LispPtr(k));

if (v != null)
return v.Get();

return null;
}

public void SetElement(LispObject k, LispObject v) throws Exception {
_map.put(new LispPtr(k), new LispPtr(v));
}

Map<LispPtr, LispPtr> _map;
}
98 changes: 98 additions & 0 deletions JavaYacas/net/sf/yacas/LispStandard.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,104 @@ public static void InternalFlatCopy(LispPtr aResult, LispPtr aOriginal) throws E
}
}

public static boolean InternalStrictTotalOrder(LispEnvironment env, LispPtr e1, LispPtr e2) throws Exception
{
if (e1.Get() == e2.Get()) {
return false;
}

if (e1.Get() == null && e2.Get() != null) {
return true;
}

if (e1.Get() != null && e2.Get() == null) {
return false;
}

BigNumber n1 = e1.Get().Number(env.Precision());
BigNumber n2 = e2.Get().Number(env.Precision());

if (n1 != null && n2 == null) {
return true;
}

if (n1 == null && n2 != null) {
return false;
}

if (n1 != null && n2 != null) {
if (n1.LessThan(n2)) {
return true;
}
if (!n1.Equals(n2)) {
return InternalStrictTotalOrder(env, e1.Get().Next(), e2.Get().Next());
}
}

String s1 = e1.Get().String();
String s2 = e2.Get().String();

if (s1 != null && s2 == null) {
return true;
}

if (s1 == null && s2 != null) {
return false;
}

if (s1 != null && s2 != null) {
int c = s1.compareTo(s2);

if (c != 0) {
return c < 0;
}

return InternalStrictTotalOrder(env, e1.Get().Next(), e2.Get().Next());
}

LispPtr l1 = e1.Get().SubList();
LispPtr l2 = e2.Get().SubList();

if (l1 != null && l2 == null) {
return true;
}

if (l1 == null && l2 != null) {
return false;
}

if (l1 != null && l2 != null) {
LispIterator i1 = new LispIterator(l1);
LispIterator i2 = new LispIterator(l2);

while (i1.GetObject() != null && i2.GetObject() != null) {
LispPtr p1 = i1.Ptr();
LispPtr p2 = i2.Ptr();

if (InternalEquals(env, p1, p2)) {
i1.GoNext();
i2.GoNext();

continue;
}

return InternalStrictTotalOrder(env, p1, p2);
}

if (i1.GetObject() != null) {
return false;
}

if (i2.GetObject() != null) {
return true;
}

return false;
}

// FIXME: deal with generics
return false;
}

public static boolean InternalEquals(LispEnvironment aEnvironment, LispPtr aExpression1, LispPtr aExpression2) throws Exception
{
Expand Down
107 changes: 104 additions & 3 deletions JavaYacas/net/sf/yacas/MathCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ public void AddCommands(LispEnvironment aEnvironment)
aEnvironment.CoreCommands().put(
"=",
new YacasEvaluator(new LispEquals(),2, YacasEvaluator.Fixed|YacasEvaluator.Function));
aEnvironment.CoreCommands().put(
"StrictTotalOrder",
new YacasEvaluator(new LispStrictTotalOrder(), 2, YacasEvaluator.Fixed|YacasEvaluator.Function));
aEnvironment.CoreCommands().put(
"LessThan",
new YacasEvaluator(new LispLessThan(),2, YacasEvaluator.Fixed|YacasEvaluator.Function));
Expand Down Expand Up @@ -434,6 +437,18 @@ public void AddCommands(LispEnvironment aEnvironment)
aEnvironment.CoreCommands().put(
"Array'Set",
new YacasEvaluator(new GenArraySet(),3, YacasEvaluator.Fixed|YacasEvaluator.Function));
aEnvironment.CoreCommands().put(
"Association'Create",
new YacasEvaluator(new GenAssociationCreate(),0, YacasEvaluator.Fixed|YacasEvaluator.Function));
aEnvironment.CoreCommands().put(
"Association'Size",
new YacasEvaluator(new GenAssociationSize(),1, YacasEvaluator.Fixed|YacasEvaluator.Function));
aEnvironment.CoreCommands().put(
"Association'Get",
new YacasEvaluator(new GenAssociationGet(),2, YacasEvaluator.Fixed|YacasEvaluator.Function));
aEnvironment.CoreCommands().put(
"Association'Set",
new YacasEvaluator(new GenAssociationSet(),3, YacasEvaluator.Fixed|YacasEvaluator.Function));
aEnvironment.CoreCommands().put(
"CustomEval",
new YacasEvaluator(new LispCustomEval(),4, YacasEvaluator.Fixed|YacasEvaluator.Macro));
Expand Down Expand Up @@ -1183,7 +1198,7 @@ class LispToString extends YacasEvalCaller
@Override
public void Eval(LispEnvironment aEnvironment,int aStackTop) throws Exception
{
StringWriter newOutput = new StringWriter();
StringWriter newOutput = new StringWriter();
Writer previous = aEnvironment.iCurrentOutput;
aEnvironment.iCurrentOutput = newOutput;
try
Expand All @@ -1192,7 +1207,7 @@ public void Eval(LispEnvironment aEnvironment,int aStackTop) throws Exception
aEnvironment.iEvaluator.Eval(aEnvironment, RESULT(aEnvironment, aStackTop), ARGUMENT(aEnvironment, aStackTop, 1));

newOutput.close();

//Return the result
RESULT(aEnvironment, aStackTop).Set(LispAtom.New(aEnvironment,aEnvironment.HashTable().LookUpStringify(newOutput.toString())));
}
Expand Down Expand Up @@ -2143,6 +2158,20 @@ public void Eval(LispEnvironment aEnvironment,int aStackTop) throws Exception
}
}

class LispStrictTotalOrder extends YacasEvalCaller {

@Override
public void Eval(LispEnvironment aEnvironment, int aStackTop) throws Exception {
LispPtr evaluated1 = new LispPtr();
evaluated1.Set(ARGUMENT(aEnvironment, aStackTop, 1).Get());
LispPtr evaluated2 = new LispPtr();
evaluated2.Set(ARGUMENT(aEnvironment, aStackTop, 2).Get());

LispStandard.InternalBoolean(aEnvironment, RESULT(aEnvironment, aStackTop),
LispStandard.InternalStrictTotalOrder(aEnvironment, evaluated1, evaluated2));
}
}

abstract class LispLexCompare2
{
abstract boolean lexfunc(String f1, String f2, LispHashTable aHashTable,int aPrecision);
Expand Down Expand Up @@ -3314,6 +3343,78 @@ public void Eval(LispEnvironment aEnvironment,int aStackTop) throws Exception
}
}

class GenAssociationCreate extends YacasEvalCaller {

@Override
public void Eval(LispEnvironment env, int stack_top) throws Exception {
AssociationClass a = new AssociationClass(env);
RESULT(env, stack_top).Set(LispGenericClass.New(a));
}
}

class GenAssociationSize extends YacasEvalCaller {

@Override
public void Eval(LispEnvironment aEnvironment, int aStackTop) throws Exception {
LispPtr evaluated = new LispPtr();
evaluated.Set(ARGUMENT(aEnvironment, aStackTop, 1).Get());

GenericClass gen = evaluated.Get().Generic();
LispError.CHK_ARG_CORE(aEnvironment, aStackTop, gen != null, 1);
LispError.CHK_ARG_CORE(aEnvironment, aStackTop, gen.TypeName().equals("\"Association\""), 1);
int size = ((AssociationClass)gen).Size();
RESULT(aEnvironment, aStackTop).Set(LispAtom.New(aEnvironment, "" + size));
}
}

class GenAssociationGet extends YacasEvalCaller {

@Override
public void Eval(LispEnvironment env, int stack_top) throws Exception {
LispPtr evaluated = new LispPtr();
evaluated.Set(ARGUMENT(env, stack_top, 1).Get());

GenericClass gen = evaluated.Get().Generic();
LispError.CHK_ARG_CORE(env, stack_top, gen != null, 1);
LispError.CHK_ARG_CORE(env, stack_top, gen.TypeName().equals("\"Association\""), 1);

LispPtr k = new LispPtr();
k.Set(ARGUMENT(env, stack_top, 2).Get());

LispError.CHK_ARG_CORE(env, stack_top, k.Get() != null, 2);

LispObject v = ((AssociationClass)gen).GetElement(k.Get());

if (v != null)
RESULT(env, stack_top).Set(v.Copy(false));
else
RESULT(env, stack_top).Set(LispAtom.New(env, "Undefined"));
}
}

class GenAssociationSet extends YacasEvalCaller {

@Override
public void Eval(LispEnvironment aEnvironment, int aStackTop) throws Exception {
LispPtr evaluated = new LispPtr();
evaluated.Set(ARGUMENT(aEnvironment, aStackTop, 1).Get());

GenericClass gen = evaluated.Get().Generic();
LispError.CHK_ARG_CORE(aEnvironment, aStackTop, gen != null, 1);
LispError.CHK_ARG_CORE(aEnvironment, aStackTop, gen.TypeName().equals("\"Association\""), 1);

LispPtr k = new LispPtr();
k.Set(ARGUMENT(aEnvironment, aStackTop, 2).Get());

LispError.CHK_ARG_CORE(aEnvironment, aStackTop, k.Get() != null, 2);

LispPtr v = new LispPtr();
v.Set(ARGUMENT(aEnvironment, aStackTop, 3).Get());
((AssociationClass)gen).SetElement(k.Get(), v.Get());
LispStandard.InternalTrue(aEnvironment, RESULT(aEnvironment, aStackTop));
}
}

class LispCustomEval extends YacasEvalCaller
{
@Override
Expand Down Expand Up @@ -3903,7 +4004,7 @@ public void Eval(LispEnvironment aEnvironment,int aStackTop) throws Exception
aEnvironment.iInputStatus.RestoreFrom(oldStatus);

resultStream.close();

RESULT(aEnvironment, aStackTop).Set(LispAtom.New(aEnvironment, resultStream.toString()));
}
}
Expand Down

0 comments on commit 8185ce4

Please sign in to comment.