diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 115b840..01d0e81 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -7,21 +7,21 @@
-
-
-
+
+
+
+
+
-
+
+
+
-
-
-
-
-
+
@@ -85,7 +85,18 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -1241,13 +1228,6 @@
-
-
-
-
-
-
-
@@ -1291,14 +1271,6 @@
-
-
-
-
-
-
-
-
@@ -1330,7 +1302,7 @@
-
+
@@ -1390,114 +1362,188 @@
-
+
-
-
+
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
+
+
+
+
-
+
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
diff --git a/samples/complex.txt b/samples/complex.txt
deleted file mode 100644
index 4fe5211..0000000
--- a/samples/complex.txt
+++ /dev/null
@@ -1,10 +0,0 @@
-> (anObject copy) rename "TraitsComplex"
-> (anObject copy) rename "Complex"
-> Complex.real = 0.0
-> Complex.img = 0.0
-> Complex.parent = TraitsComplex
-> TraitsComplex.str = {: (((self.real str) + " + ") + (self.img str)) + "i" }
-> TraitsComplex.isReal = {: self.img == 0.0 }
-> TraitsComplex.add = {c: self.real = (self.real + c.real); self.img = (self.img + c.img) }
-> TraitsComplex.sub = {c: self.real = (self.real - c.real); self.img = (self.img - c.img) }
-> TraitsComplex.mul = {c: self.real = ((self.real * c.real) - (self.img * c.img)); self.img = ((self.img * c.real) + (self.real * c.img)) }
diff --git a/samples/init.txt b/samples/init.txt
deleted file mode 100644
index b230b0d..0000000
--- a/samples/init.txt
+++ /dev/null
@@ -1,65 +0,0 @@
-Pooi [Prototype-based, object-oriented interpreter]
-
-type in your message
-try "Root list", "help" or "about" to start
-
-
-> (anObject copy) rename "Point"
-anObject was copied into Root as 'Root.anObject1'
-Root.anObject1 renamed to Root.Point
-
-
-> (anObject copy) rename "TraitsPoint"
-anObject was copied into Root as 'Root.anObject2'
-Root.anObject2 renamed to Root.TraitsPoint
-
-
-> Point.x = 0
-'x' set in Root.Point
-
-
-> Point.y = 0
-'y' set in Root.Point
-
-
-> (anObject copy) rename "Persona"
-anObject was copied into Root as 'Root.anObject3'
-Root.anObject3 renamed to Root.Persona
-
-
-> Point.parent = TraitsPoint
-'parent' set in Root.Point
-
-
-> Persona.edad = 42
-'edad' set in Root.Persona
-
-
-> persona.nombre = "Baltasar García Perez-Schofield"
-Error: Attribute not found: 'persona' in 'Root'
-
-> Persona.nombre = "Baltasar García Perez-Schofield"
-'nombre' set in Root.Persona
-
-
-> (anObject copy) rename "TraitsPersona"
-anObject was copied into Root as 'Root.anObject4'
-Root.anObject4 renamed to Root.TraitsPersona
-
-
-> Persona.parent = TraitsPersona
-'parent' set in Root.Persona
-
-
-> TraitsPoint.shift = {d: self.x = (self.x + d); self.y = (self.y + d)}
-'shift' set in Root.TraitsPoint
-
-
-> Point shift 5
-Root.Point.x and Root.bin.lit16 added, giving 5
-'x' set in Root.Point
-Root.Point.y and Root.bin.lit19 added, giving 5
-'y' set in Root.Point
-
-
-
diff --git a/samples/math.txt b/samples/math.txt
index 5d85863..9c0a077 100644
--- a/samples/math.txt
+++ b/samples/math.txt
@@ -1,5 +1,3 @@
> (anObject copy) rename "Math"
> Math.PI = 3.1416
> Math.sqr = {x: x * x }
-> Math.sqrt = {x: x * x }
->
diff --git a/src/com/devbaltasarq/pooi/core/Evaluable.java b/src/com/devbaltasarq/pooi/core/Evaluable.java
index 7de75da..40491b5 100644
--- a/src/com/devbaltasarq/pooi/core/Evaluable.java
+++ b/src/com/devbaltasarq/pooi/core/Evaluable.java
@@ -1,11 +1,18 @@
package com.devbaltasarq.pooi.core;
+import com.devbaltasarq.pooi.core.evaluables.Command;
+
/**
* Represents all evaluable parts inside a method
* User: baltasarq
* Date: 11/19/12
*/
public abstract class Evaluable {
-
- public abstract String toString();
+ /** Determines whether this evaluable is just a POP or not.
+ * @return true when it is a POP, false otherwise.
+ */
+ public boolean isPopTask()
+ {
+ return false;
+ }
}
diff --git a/src/com/devbaltasarq/pooi/core/Interpreter.java b/src/com/devbaltasarq/pooi/core/Interpreter.java
index 7d2b734..508f2b2 100644
--- a/src/com/devbaltasarq/pooi/core/Interpreter.java
+++ b/src/com/devbaltasarq/pooi/core/Interpreter.java
@@ -272,7 +272,7 @@ protected ObjectBag execute(InterpretedMethod method, ObjectBag self, Evaluable[
// Execute command stack
for(Evaluable evaluable: method.getCmds()) {
- Command command = (Command) evaluable;
+ final Command command = (Command) evaluable;
// Substitute __POP's in params
final int numCmdParams = command.getNumArguments();
@@ -284,8 +284,7 @@ protected ObjectBag execute(InterpretedMethod method, ObjectBag self, Evaluable[
for(int i = numCmdParams -1; i >= 0; --i) {
Evaluable param = cmdParams[ i ];
- if ( param.toString().equals( Parser.PopTask ))
- {
+ if ( param.isPopTask() ) {
param = stack.getTop();
stack.pop();
}
@@ -306,7 +305,7 @@ protected ObjectBag execute(InterpretedMethod method, ObjectBag self, Evaluable[
// In reference
ref = command.getReference();
- if ( ref.toString().equals( Parser.PopTask ) ) {
+ if ( ref.isPopTask() ) {
ref = stack.getTop();
stack.pop();
}
diff --git a/src/com/devbaltasarq/pooi/core/ObjectBag.java b/src/com/devbaltasarq/pooi/core/ObjectBag.java
index 0820fa6..1479069 100644
--- a/src/com/devbaltasarq/pooi/core/ObjectBag.java
+++ b/src/com/devbaltasarq/pooi/core/ObjectBag.java
@@ -587,7 +587,8 @@ public void set(String name, ObjectBag obj) throws InterpretError
// Change the container of the pointed object, if appropriated
if ( !isParent
- && obj instanceof ValueObject )
+ && obj instanceof ValueObject
+ && obj.getContainer() == this.getRuntime().getLiteralsContainer() )
{
obj.setContainer( this );
}
diff --git a/src/com/devbaltasarq/pooi/core/Parser.java b/src/com/devbaltasarq/pooi/core/Parser.java
index 16d4ee0..ab5a10f 100644
--- a/src/com/devbaltasarq/pooi/core/Parser.java
+++ b/src/com/devbaltasarq/pooi/core/Parser.java
@@ -21,8 +21,6 @@
* Date: 11/16/12
*/
public class Parser {
- public static final String PopTask = "__POP";
-
private static void clean(ArrayList toret)
{
for(int i = 0; i < toret.size(); ++i)
@@ -32,7 +30,7 @@ private static void clean(ArrayList toret)
if ( evaluable instanceof Command ) {
Command cmd = (Command) evaluable;
- if ( cmd.getReference().toString().equals( PopTask )
+ if ( cmd.getReference().toString().equals( Reserved.PopTask )
&& cmd.getMessage().isEmpty() )
{
toret.remove( i );
@@ -49,7 +47,7 @@ private static void clean(ArrayList toret)
* ref mth param1 ...paramN; ref mth param1 ...paramN; ...
* @param order The input given by the user
* @return A vector of Command object representing user's intentions
- * @throws com.devbaltasarq.pooi.core.exceps.InterpretError
+ * @throws com.devbaltasarq.pooi.core.Interpreter.InterpretError
*/
public static Command[] parseOrder(Runtime rt, String order) throws InterpretError {
ArrayList toret = new ArrayList();
@@ -93,7 +91,7 @@ public static Command[] parseOrder(Runtime rt, String order) throws InterpretErr
* @param lex the lexer with the order given by the user
* @param cmds the orders stack so far
* @return A command object that represents user's intentions
- * @throws com.devbaltasarq.pooi.core.exceps.InterpretError when parsing is not possible
+ * @throws com.devbaltasarq.pooi.core.Interpreter.InterpretError when parsing is not possible
*/
public static Command parseCommand(Runtime rt, Lexer lex, ArrayList cmds) throws InterpretError
{
@@ -105,7 +103,7 @@ public static Command parseCommand(Runtime rt, Lexer lex, ArrayList cmd
// Set the attributes of the reference
if ( lex.getCurrentChar() == '(' ) {
- toret.setReference( new Reference( new String[]{ PopTask } ) );
+ toret.setReference( new Reference( new String[]{ Reserved.PopTask } ) );
lex.advance();
cmds.add( parseCommand( rt, lex, cmds ) );
} else {
@@ -229,7 +227,7 @@ private static Command parsePartialCommand(Runtime rt, Command toret, Lexer lex
{
if ( lex.getCurrentChar() == '(' ) {
lex.advance();
- params.add( new Reference( PopTask ) );
+ params.add( new Reference( Reserved.PopTask ) );
cmds.add( parseCommand( rt, lex, cmds ) );
}
else
@@ -251,7 +249,7 @@ private static Command parsePartialCommand(Runtime rt, Command toret, Lexer lex
if ( toret.isValid()
&& toret.getMessage().trim().isEmpty()
- && !toret.getReference().toString().equals( PopTask ) )
+ && !toret.getReference().toString().equals( Reserved.PopTask ) )
{
toret.setMessage( NativeMethodStr.EtqMthToString );
}
diff --git a/src/com/devbaltasarq/pooi/core/Reserved.java b/src/com/devbaltasarq/pooi/core/Reserved.java
index a3e8122..98f5373 100644
--- a/src/com/devbaltasarq/pooi/core/Reserved.java
+++ b/src/com/devbaltasarq/pooi/core/Reserved.java
@@ -9,6 +9,7 @@ public class Reserved {
public static final String ParentAttribute = "parent";
public static final String SetMethod = "set";
+ public static final String PopTask = "__POP";
public static final String RootObject = "Root";
@@ -36,6 +37,7 @@ public class Reserved {
+ ' ' + AssignmentOperator + ' '
+ ' ' + DateTimeObject + ' '
+ ' ' + SetMethod + ' '
+ + ' ' + PopTask + ' '
;
public static boolean isReservedForObjects(String id)
diff --git a/src/com/devbaltasarq/pooi/core/evaluables/Attribute.java b/src/com/devbaltasarq/pooi/core/evaluables/Attribute.java
index 902eccf..0c34a84 100644
--- a/src/com/devbaltasarq/pooi/core/evaluables/Attribute.java
+++ b/src/com/devbaltasarq/pooi/core/evaluables/Attribute.java
@@ -19,7 +19,8 @@
public class Attribute extends Member {
/** Creates a new instance of Attribute */
- public Attribute(String n, ObjectBag ref) {
+ public Attribute(String n, ObjectBag ref)
+ {
super( n );
this.setReference( ref );
}
@@ -27,8 +28,8 @@ public Attribute(String n, ObjectBag ref) {
/** Changes the object this attributes points to
* @param obj The object this attribute will point to
*/
- public void setReference(ObjectBag obj) {
-
+ public void setReference(ObjectBag obj)
+ {
reference = obj;
}
diff --git a/src/com/devbaltasarq/pooi/core/evaluables/Command.java b/src/com/devbaltasarq/pooi/core/evaluables/Command.java
index 529a328..3ff9aae 100644
--- a/src/com/devbaltasarq/pooi/core/evaluables/Command.java
+++ b/src/com/devbaltasarq/pooi/core/evaluables/Command.java
@@ -1,12 +1,8 @@
-/*
- * Command.java
- * Usado para devolver una entrada descompuesta en atributos, message
- * y parámetros.
- */
-
+// Pooi (c) Baltasar 2013 - 2018 MIT License
package com.devbaltasarq.pooi.core.evaluables;
import com.devbaltasarq.pooi.core.Evaluable;
+import com.devbaltasarq.pooi.core.Reserved;
import com.devbaltasarq.pooi.core.evaluables.literals.StrLiteral;
/**
@@ -18,21 +14,14 @@
* - Args are the arguments involved.
*/
public class Command extends Evaluable {
-
- public Command()
- {
- }
-
- /**
- * @return the message
- */
+ /** @return the message */
public String getMessage()
{
return message;
}
- /**
- * @param message the message to set
+ /** Sets a new message for the command (the method to call), so "+" in x + 1..
+ * @param message the message to set
*/
public void setMessage(String message)
{
@@ -46,34 +35,33 @@ public void setMessage(String message)
}
}
- public boolean isValid()
+ @Override
+ public boolean isPopTask()
{
- return ( this.getReference() != null );
+ return ( this.getNumArguments() == 0 )
+ && ( this.getReference().toString().equals( Reserved.PopTask ) );
}
- public boolean hasMessage()
+ public boolean isValid()
{
- return !( getMessage().isEmpty() );
+ return ( this.getReference() != null );
}
- /**
- * @return the args
- */
+ /** @return the args of the command (as a Evaluable[]), so "1" in x + 1. */
public Evaluable[] getArguments() {
return args;
}
- /**
- * @param args the args to set
+ /** Changes the arguments of the command.
+ * @param args the args to set
*/
public void setArguments(Evaluable[] args)
{
this.args = args;
}
- /**
- * Returns all arguments
- * @return All arguments, as a String.
+ /** Returns all arguments
+ * @return All arguments, as a String.
*/
public String getArgumentsAsString()
{
@@ -99,16 +87,29 @@ public String getArgumentsAsString()
return toret.toString();
}
+ /** Gets the reference of the command, so "x" in x + 1.
+ * @return The reference, as an Evaluable.
+ * @see Evaluable
+ * @see Reference
+ */
public Evaluable getReference()
{
return this.ref;
}
+ /** Changes the reference of the command, , so "x" in x + 1.
+ * @param ref The new reference, as an Evaluable object (probably a Reference).
+ * @see Evaluable
+ * @see Reference
+ */
public void setReference(Evaluable ref)
{
this.ref = ref;
}
+ /** The number of arguments in the command, so "1" in X + 1.
+ * @return the number of arguments.
+ */
public int getNumArguments()
{
return this.args.length;
diff --git a/src/com/devbaltasarq/pooi/core/evaluables/Method.java b/src/com/devbaltasarq/pooi/core/evaluables/Method.java
index 927f6ca..fa0d776 100644
--- a/src/com/devbaltasarq/pooi/core/evaluables/Method.java
+++ b/src/com/devbaltasarq/pooi/core/evaluables/Method.java
@@ -72,7 +72,8 @@ public String getStringFrom(Evaluable e) throws InterpretError
* Returns a string with the formal parameters of the method
* @return a String, with the formal parameters
*/
- public String getFormalParametersAsString() {
+ public String getFormalParametersAsString()
+ {
StringBuilder toret = new StringBuilder();
for(String param: this.getFormalParameters()) {
@@ -95,17 +96,17 @@ public String getFormalParametersAsString() {
*/
public abstract Method copy();
- public Runtime getRuntime() {
+ public Runtime getRuntime()
+ {
return this.rt;
}
@Override
- public String toString() {
+ public String toString()
+ {
StringBuilder toret = new StringBuilder();
toret.append( this.getName() );
- toret.append( ' ' );
- toret.append( this.getFormalParametersAsString() );
toret.append( " = " );
toret.append( this.getMethodBodyAsString() );
diff --git a/src/com/devbaltasarq/pooi/core/evaluables/Reference.java b/src/com/devbaltasarq/pooi/core/evaluables/Reference.java
index 3058527..e956383 100644
--- a/src/com/devbaltasarq/pooi/core/evaluables/Reference.java
+++ b/src/com/devbaltasarq/pooi/core/evaluables/Reference.java
@@ -1,6 +1,8 @@
+// Pooi (c) Baltasar 2013 - 2018 MIT License
package com.devbaltasarq.pooi.core.evaluables;
import com.devbaltasarq.pooi.core.Evaluable;
+import com.devbaltasarq.pooi.core.Reserved;
import java.util.ArrayList;
@@ -10,6 +12,10 @@
* Date: 11/19/12
*/
public class Reference extends Evaluable {
+ public Reference()
+ {
+ this.attrs = new String[ 0 ];
+ }
public Reference(String id)
{
@@ -23,9 +29,13 @@ public Reference(String[] ids)
this.setAttrs( ids );
}
- public Reference()
+ @Override
+ public boolean isPopTask()
{
- this.attrs = new String[ 0 ];
+ final String[] attrs = this.getAttrs();
+
+ return ( ( attrs.length == 1 )
+ && ( attrs[ 0 ].equals( Reserved.PopTask ) ) );
}
/**
diff --git a/src/com/devbaltasarq/pooi/core/evaluables/methods/InterpretedMethod.java b/src/com/devbaltasarq/pooi/core/evaluables/methods/InterpretedMethod.java
index d40db18..37ef94a 100644
--- a/src/com/devbaltasarq/pooi/core/evaluables/methods/InterpretedMethod.java
+++ b/src/com/devbaltasarq/pooi/core/evaluables/methods/InterpretedMethod.java
@@ -154,7 +154,6 @@ public String getMethodBodyAsString()
{
StringBuilder toret = new StringBuilder();
Stack stack = new Stack<>();
- ArrayList positions = new ArrayList<>();
// Add params
toret.append( "{ " );
@@ -163,59 +162,49 @@ public String getMethodBodyAsString()
// Add commands
for(Command cmd: this.getCmds()) {
- toret.append( cmd.toString() );
- toret.append( "; " );
- }
-/*
- // Add commands
- for(Command cmd: this.getCmds()) {
- String replaceCmd;
- String cmdAsStr = cmd.toString();
-
- // Find all POP occurrences
- positions.clear();
- int popPos = cmdAsStr.lastIndexOf( Parser.PopTask );
- do {
- while( popPos > -1 ) {
- positions.add( popPos );
- cmdAsStr.lastIndexOf( Parser.PopTask, popPos - Parser.PopTask.length() );
- }
-
- for(int pos: positions) {
- // Process possible targets
- // pos += replaceCmd.length();
- // pos = cmdAsStr.indexOf( Parser.PopTask, pos );
- replaceCmd = stack.pop();
- cmdAsStr = cmdAsStr.substring( 0, pos )
- + replaceCmd
- + cmdAsStr.substring( pos + Parser.PopTask.length(), cmdAsStr.length() );
- }
-
- popPos = cmdAsStr.lastIndexOf( Parser.PopTask );
- } while( popPos > -1 );
-/*
- // Process the reference
- if ( cmd.getReference().toString().equals( Parser.PopTask ) ) {
- replaceCmd = stack.pop();
- cmdAsStr = cmdAsStr.substring( 0, popPos )
- + replaceCmd
- + cmdAsStr.substring( popPos + Parser.PopTask.length(), cmdAsStr.length() );
- }
-*/
-/*
- // Push command
- stack.push( cmdAsStr );
+ stack.push( replacePop( cmd.toString(), stack ) );
}
- // Dump the remaining in natural order (nearly never executed)
+ // Dump the remaining
+ String separator = "";
for(String strCmd: stack) {
+ toret.append( separator );
toret.append( strCmd );
+ separator = "; ";
}
-*/
- toret.append( "}" );
+
+ toret.append( " }" );
return toret.toString();
}
+ private static Integer[] findPops(String cmdAsStr)
+ {
+ ArrayList positions = new ArrayList<>();
+ int popPos = cmdAsStr.lastIndexOf( Reserved.PopTask );
+
+ while( popPos > -1 ) {
+ positions.add( popPos );
+ popPos = cmdAsStr.lastIndexOf( Reserved.PopTask, popPos - Reserved.PopTask.length() );
+ }
+
+ return positions.toArray( new Integer[ positions.size() ] );
+ }
+
+ private static String replacePop(String cmdAsStr, Stack stack)
+ {
+ Integer[] positions = findPops( cmdAsStr );
+
+ for(int pos: positions) {
+ String replaceCmd = replacePop( stack.pop(), stack );
+
+ cmdAsStr = cmdAsStr.substring( 0, pos )
+ + replaceCmd
+ + cmdAsStr.substring( pos + Reserved.PopTask.length(), cmdAsStr.length() );
+ }
+
+ return cmdAsStr;
+ }
+
public static String createNewMethodId()
{
String toret = DefaultMethodId;
@@ -227,7 +216,8 @@ public static String createNewMethodId()
}
@Override
- public int getNumParams() {
+ public int getNumParams()
+ {
return this.formalParams.length;
}
diff --git a/src/com/devbaltasarq/pooi/core/objs/ValueObject.java b/src/com/devbaltasarq/pooi/core/objs/ValueObject.java
index cc1a02f..04bfe24 100644
--- a/src/com/devbaltasarq/pooi/core/objs/ValueObject.java
+++ b/src/com/devbaltasarq/pooi/core/objs/ValueObject.java
@@ -33,5 +33,3 @@ public String list()
public abstract Object getValueObject();
}
-
-