Skip to content

Commit

Permalink
Improved session retrieval: messages span several lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltasarq committed Dec 3, 2018
1 parent ab4f472 commit 3be156f
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 55 deletions.
12 changes: 10 additions & 2 deletions samples/point.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
> (TraitsPoint createChild) rename "Point"
> Point.x = 0
> Point.y = 0
> TraitsPoint.shift = { delta: self.x = ( self.x + delta ); self.y = ( self.y + delta ) }
> TraitsPoint.move = { x y: self.x = x; self.y = y }
> TraitsPoint.shift = { delta:
self.x = ( self.x + delta );
self.y = ( self.y + delta )
}

> TraitsPoint.move = { x y:
self.x = x;
self.y = y
}

> TraitsPoint.str = {: ( ( self.x str ) + ", " ) + ( self.y str ) }
> (Point copy) rename "p1"
> p1 str
Expand Down
7 changes: 2 additions & 5 deletions src/com/devbaltasarq/pooi/core/AppInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
// Pooi (c) 2014 Baltasar MIT License <jbgarcia@uvigo.es>

package com.devbaltasarq.pooi.core;

Expand All @@ -13,7 +10,7 @@ public class AppInfo {
public static final String Name = "Pooi";
public static final String Email = "jbgarcia@uvigo.es";
public static final String Author = "Baltasar García Perez-Schofield";
public static final String Version = "2.2 20180605";
public static final String Version = "2.2.1 20181203";
public static final String License = "MIT License: https://opensource.org/licenses/MIT";
public static final String SessionFileExt = ".txt";
public static final String ScriptFileExt = ".poi";
Expand Down
146 changes: 120 additions & 26 deletions src/com/devbaltasarq/pooi/core/Interpreter.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Pooi (c) 2014 Baltasar MIT License <jbgarcia@uvigo.es>

package com.devbaltasarq.pooi.core;

import com.devbaltasarq.pooi.core.evaluables.Attribute;
Expand All @@ -9,6 +11,7 @@
import com.devbaltasarq.pooi.core.objs.ObjectBool;

import java.io.*;
import java.util.ArrayList;

/**
* The interpreter, making sense of commands sent.
Expand Down Expand Up @@ -119,7 +122,8 @@ public void interpretScript(String fileName, InputStream stream) throws Interpre
}
}

public void reset(Runtime rt) throws InterpretError {
public void reset(Runtime rt) throws InterpretError
{
this.rt = rt;
this.error = false;
this.createInfoObject();
Expand Down Expand Up @@ -371,6 +375,13 @@ protected ObjectBag execute(InterpretedMethod method, ObjectBag self, Evaluable[
return toret;
}

/** Finds the correct reference in param val,
* againts the method, the current object (self), and the runtime.
* @param rt the runtime the interpreter executes against.
* @param self the object executing the method.
* @param method the method that it is being executed.
* @param val the value to find.
*/
protected Evaluable findCorrectReferenceInParam(Runtime rt, ObjectBag self, InterpretedMethod method, Evaluable val)
throws InterpretError
{
Expand Down Expand Up @@ -407,45 +418,113 @@ protected Evaluable findCorrectReferenceInParam(Runtime rt, ObjectBag self, Inte
return toret;
}

/** @return whether there was an error or not. */
public boolean getError()
{
return error;
}

public String loadSession(String fileName) {
/** Loads a complete session.
* @param fileName the file name to load.
*/
public String loadSession(String fileName)
{
final String SESSION_OK = "\n\n*** Session '%s' restored ok. ***\n\n";
final String SESSION_ERROR = "\n\n*** Session '%s' restored with errors ***.\n\n";
final String SESSION_FAILED = "\n*** Session '%s' restoration failed ***\n\n";
final String FILE_NAME = new File( fileName ).getName();
StringBuilder toret = new StringBuilder();
String lin;
String[] source = this.loadSessionFile( fileName, toret );

try {
BufferedReader session = new BufferedReader( new FileReader( new File( fileName ) ) );
if ( source.length > 0 ) {
// Interpret all instructions
for(String instruction: source) {
toret.append( ">" + instruction );
toret.append( '\n' );
toret.append( interpret( instruction ) );
toret.append( "\n\n" );

if ( this.getError() ) {
break;
}
}

if ( this.getError() ) {
toret.append( String.format( SESSION_ERROR, FILE_NAME ) );
} else {
toret.append( String.format( SESSION_OK, FILE_NAME ) );
}
} else {
toret.append( String.format( SESSION_FAILED, FILE_NAME ) );
}

return toret.toString();
}

/** Loads a complete session.
* @param fileName the file name to load.
* @param console the future messages to show in the console
*/
private String[] loadSessionFile(String fileName, StringBuilder console)
{
final ArrayList<String> toret = new ArrayList<>();
String lin;
String instruction = "";
int openBraces = 0;

try (BufferedReader session = new BufferedReader( new FileReader(
new File( fileName ) ) ) )
{
lin = session.readLine();
while( lin != null ) {
lin = lin.trim();

if ( lin.length() > 1 ) {
if ( lin.charAt( 0 ) == '>' ) {
toret.append( lin );
toret.append( '\n' );
toret.append(
interpret( lin.substring( 1, lin.length() ) )
);
toret.append( "\n\n" );

// Should this line be considered?
if ( lin.length() > 0
&& ( lin.charAt( 0 ) == '>'
|| openBraces > 0 ) )
{
// Remove the opening '>'
if ( openBraces <= 0
&& lin.charAt( 0 ) == '>' )
{
lin = lin.substring( 1 );
}

// Check for braces
for(char ch: lin.toCharArray()) {
if ( ch == '{' ) {
++openBraces;
}
else
if ( ch == '}' ) {
--openBraces;
}
}

// Add it
instruction += " " + lin;
if ( openBraces <= 0 ) {
openBraces = 0;
toret.add( instruction );
instruction = "";
}
}

lin = session.readLine();
}

session.close();
toret.append( "\n\nsession restored ok.\n\n");
} catch(Exception e) {
toret.append( "\nsession failed to restore\n\n" );
}
toret.clear();
console.append( "\nERROR: " + e.getMessage() + "\n" );
}

return toret.toString();
return toret.toArray( new String[ 0 ] );
}

/** Checks whether the interpreter is in verbose mode or not.
* Create the info object if it does not exist.
* @return true if the interpreter is in verbose mode, or false.
*/
public boolean isVerbose() throws InterpretError
{
final InterpreterCfg cfg = this.getConfiguration();
Expand All @@ -469,6 +548,10 @@ public boolean isVerbose() throws InterpretError
return cfg.isVerbose();
}

/** Puts the interpreter in verbose mode or not.
* Create the info object if it does not exist.
* @param flag true to put the interpreter in verbose mode.
*/
public void setVerbose(boolean flag) throws InterpretError {
Attribute attrVerbose = null;
ObjectBag info = this.getObjInfo();
Expand All @@ -483,19 +566,30 @@ public void setVerbose(boolean flag) throws InterpretError {
this.getObjInfo().set( EtqInfoObjAttrVerbose, this.getRuntime().createBool( flag ) );
this.getConfiguration().setVerbose( flag );
}

public InterpreterCfg getConfiguration() {
return this.cfg;

/** @return the info object.
* @see Runtime.
*/
public ObjectBag getObjInfo()
{
return this.objInfo;
}

public ObjectBag getObjInfo() {
return this.objInfo;
/** @return the main configuration of the app. */
public InterpreterCfg getConfiguration()
{
return this.cfg;
}

public boolean hasGui() {
/** @return true if the app is executing with GUI, false otherwise. */
public boolean hasGui()
{
return this.getConfiguration().hasGui();
}

/** @return the runtime for the interpreter.
* @see Runtime.
*/
public Runtime getRuntime()
{
return this.rt;
Expand Down
18 changes: 9 additions & 9 deletions src/com/devbaltasarq/pooi/core/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void advance()

public void advance(int value)
{
this.setPos( getPos() + value );
this.setPos( this.getPos() + value );
}

public boolean isWhiteSpace()
Expand Down Expand Up @@ -54,20 +54,20 @@ public String getToken()
char ch;

token = "";
skipSpaces();
ch = getCurrentChar();
this.skipSpaces();
ch = this.getCurrentChar();


while( ( Character.isLetterOrDigit( ch )
|| ch == '_' )
&& getPos() < getLine().length() )
&& this.getPos() < this.getLine().length() )
{
token += ch;
advance();
ch = getCurrentChar();
}

skipSpaces();
this.skipSpaces();
return token;
}

Expand Down Expand Up @@ -138,13 +138,13 @@ public String getLiteral(char openDelimiter, char endDelimiter)
}
}

advance();
this.advance();
}

advance();
this.advance();
}

skipSpaces();
this.skipSpaces();
return token;
}

Expand All @@ -153,7 +153,7 @@ public char getCurrentChar()
char toret = '\0';

if ( !this.isEOL() ) {
toret = getLine().charAt( getPos() );
toret = this.getLine().charAt( this.getPos() );
}

return toret;
Expand Down
24 changes: 11 additions & 13 deletions src/com/devbaltasarq/pooi/core/Persistence.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ObjectPersistence.java
// Pooi (c) 2014 Baltasar MIT License <jbgarcia@uvigo.es>

package com.devbaltasarq.pooi.core;

Expand All @@ -9,32 +9,30 @@
* @author baltasarq
*/
public abstract class Persistence {

public static final String ObjectNameMember = "__name__";

protected ObjectBag[] objs = null;

public ObjectBag getObj()
public Persistence(ObjectBag obj)
{
return this.objs[ 0 ];
this.objs = new ObjectBag[]{ obj };
}

public ObjectBag[] getObjs()
public Persistence(ObjectBag[] objs)
{
return this.objs;
this.objs = objs;
}

public Persistence(ObjectBag obj)
public ObjectBag getObj()
{
this.objs = new ObjectBag[]{ obj };
return this.objs[ 0 ];
}

public Persistence(ObjectBag[] objs)
public ObjectBag[] getObjs()
{
this.objs = objs;
return this.objs;
}

public abstract ObjectBag[] load(File path);
public abstract boolean save(File path);

protected ObjectBag[] objs = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.devbaltasarq.pooi.core.evaluables.methods.NativeMethod;
import com.devbaltasarq.pooi.core.Interpreter.InterpretError;

// TODO: Should not be deletable.
/**
* Copying objects
* @author baltasarq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.devbaltasarq.pooi.core.evaluables.methods.NativeMethod;
import com.devbaltasarq.pooi.core.Interpreter.InterpretError;

// TODO: Should not be deletable.

/**
*
* @author baltasarq
Expand Down

0 comments on commit 3be156f

Please sign in to comment.