Skip to content

Commit

Permalink
Correct outputting of method bodies, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltasarq committed Jan 17, 2018
1 parent 825caae commit 1618507
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 309 deletions.
306 changes: 176 additions & 130 deletions .idea/workspace.xml

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions samples/complex.txt

This file was deleted.

65 changes: 0 additions & 65 deletions samples/init.txt

This file was deleted.

2 changes: 0 additions & 2 deletions samples/math.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
> (anObject copy) rename "Math"
> Math.PI = 3.1416
> Math.sqr = {x: x * x }
> Math.sqrt = {x: x * x }
>
11 changes: 9 additions & 2 deletions src/com/devbaltasarq/pooi/core/Evaluable.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 3 additions & 4 deletions src/com/devbaltasarq/pooi/core/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion src/com/devbaltasarq/pooi/core/ObjectBag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
14 changes: 6 additions & 8 deletions src/com/devbaltasarq/pooi/core/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
* Date: 11/16/12
*/
public class Parser {
public static final String PopTask = "__POP";

private static void clean(ArrayList<Command> toret)
{
for(int i = 0; i < toret.size(); ++i)
Expand All @@ -32,7 +30,7 @@ private static void clean(ArrayList<Command> 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 );
Expand All @@ -49,7 +47,7 @@ private static void clean(ArrayList<Command> 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<Command> toret = new ArrayList<Command>();
Expand Down Expand Up @@ -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<Command> cmds) throws InterpretError
{
Expand All @@ -105,7 +103,7 @@ public static Command parseCommand(Runtime rt, Lexer lex, ArrayList<Command> 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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 );
}
Expand Down
2 changes: 2 additions & 0 deletions src/com/devbaltasarq/pooi/core/Reserved.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -36,6 +37,7 @@ public class Reserved {
+ ' ' + AssignmentOperator + ' '
+ ' ' + DateTimeObject + ' '
+ ' ' + SetMethod + ' '
+ ' ' + PopTask + ' '
;

public static boolean isReservedForObjects(String id)
Expand Down
7 changes: 4 additions & 3 deletions src/com/devbaltasarq/pooi/core/evaluables/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
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 );
}

/** 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;
}

Expand Down
57 changes: 29 additions & 28 deletions src/com/devbaltasarq/pooi/core/evaluables/Command.java
Original file line number Diff line number Diff line change
@@ -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 <jbgarcia@uvigo.es>
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;

/**
Expand All @@ -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)
{
Expand All @@ -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()
{
Expand All @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions src/com/devbaltasarq/pooi/core/evaluables/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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() );

Expand Down
Loading

0 comments on commit 1618507

Please sign in to comment.