Skip to content

Commit

Permalink
New os and datetime objets created
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltasarq committed Jan 29, 2016
1 parent 8a84625 commit c8ddaeb
Show file tree
Hide file tree
Showing 8 changed files with 521 additions and 160 deletions.
514 changes: 357 additions & 157 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/core/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void advance(int value)

public boolean isWhiteSpace()
{
return Character.isWhitespace( this.getCurrentChar() );
return ( Character.isWhitespace( this.getCurrentChar() ) );
}

public void skipSpaces()
Expand Down
2 changes: 1 addition & 1 deletion src/core/ObjectBag.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public String list()
toret.append( "\n" );
}

return toret.append( "\n}" ).toString();
return toret.append( '}' ).toString();
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/core/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import core.evaluables.literals.IntLiteral;
import core.evaluables.literals.RealLiteral;
import core.evaluables.literals.StrLiteral;
import core.evaluables.method.InterpretedMethod;
import core.evaluables.method.nativemethods.*;
import core.exceps.AttributeNotFound;
import core.exceps.InterpretError;
import core.objs.*;

import java.util.Calendar;

/**
* The runtime, i.e. preexisting objects, root, etc.
* User: baltasarq
Expand All @@ -27,16 +30,19 @@ public final class Runtime {
public static final String EtqNameBool = Reserved.BoolObject;
public static final String EtqNameReal = Reserved.RealObject;
public static final String EtqNameStr = Reserved.StrObject;
public static final String EtqNameDateTime = "DateTime";
public static final String EtqNameAnObject = "anObject";
public static final String EtqNameLiterals = "bin";
public static final String EtqLit = "lit";

private Runtime() throws InterpretError
{
// The inheritance root and the main container
this.absParent = new ObjectParent( EtqTopParentObject );
this.root = new ObjectRoot( EtqNameRoot, absParent );
this.root.set( EtqTopParentObject, this.absParent );

// Main "type" objects
this.str = new SysObject( EtqNameStr, absParent, root );
this.root.set( EtqNameStr, this.str );

Expand All @@ -49,12 +55,21 @@ private Runtime() throws InterpretError
this.bool = new SysObject( EtqNameBool, absParent, root );
this.root.set( EtqNameBool, this.bool );

this.dateTime = new SysObject( EtqNameDateTime, absParent, root );
this.root.set( EtqNameDateTime, this.dateTime );

// The first prototype
this.anObject = new ObjectBag( EtqNameAnObject, absParent, root );
this.root.set( EtqNameAnObject, this.anObject );

// An object to hold literals
this.literals = new SysObject( EtqNameLiterals, absParent, root );
this.root.set( EtqNameLiterals, this.literals );

// The operating system rep
this.os = new ObjectOs( absParent, root );
this.root.set( ObjectOs.Name, this.os );

this.addMethodsToRuntimeObjects();
this.getLiteralsContainer().clear( false );
}
Expand Down Expand Up @@ -134,6 +149,28 @@ public ObjectStr createString(String id, String value) throws InterpretError
return toret;
}

public ObjectDateTime createDateTime(long year, long month, long day, long hour, long minute, long second)
throws InterpretError
{
final ObjectDateTime toret = new ObjectDateTime(
this.createNewLiteralName(),
this.dateTime,
this.getLiteralsContainer() );

// Date
toret.set( ObjectDateTime.EtqDay, rt.createInt( day ) );
toret.set( ObjectDateTime.EtqMonth, rt.createInt( month ) );
toret.set( ObjectDateTime.EtqYear, rt.createInt( year ) );

// Time
toret.set( ObjectDateTime.EtqSecond, rt.createInt( second ) );
toret.set( ObjectDateTime.EtqMinute, rt.createInt( minute ) );
toret.set( ObjectDateTime.EtqHour, rt.createInt( hour ) );

this.getLiteralsContainer().set( toret.getName(), toret );
return toret;
}

public ObjectInt createInt(long num) throws InterpretError
{
return this.createInt( this.createNewLiteralName(), num );
Expand Down Expand Up @@ -338,8 +375,10 @@ private String createNewLiteralName()
private final ObjectBag integer;
private final ObjectBag real;
private final ObjectBag bool;
private final ObjectBag dateTime;
private final ObjectBag anObject;
private final ObjectBag literals;
private final ObjectOs os;

private static Runtime rt;
private static int numLiterals = 0;
Expand Down
9 changes: 8 additions & 1 deletion src/core/evaluables/method/InterpretedMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ protected String extractParams(String cmds) throws InterpretError
&& !lex.isEOL() )
{
formalParamsList.add( lex.getToken() );

// Skip spaces or commas
lex.skipSpaces();
if ( lex.getCurrentChar() == ',' ) {
lex.advance();
lex.skipSpaces();
}
}

// Check ':' and remove it
Expand Down Expand Up @@ -153,7 +159,8 @@ public String toString()

// Add params
for(String param: this.getFormalParams()) {
toret.append( param );
toret.append( param );
toret.append( ' ' );
}

toret.append( ": " );
Expand Down
45 changes: 45 additions & 0 deletions src/core/evaluables/method/nativemethods/NativeMethodOsNow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package core.evaluables.method.nativemethods;

import core.Evaluable;
import core.ObjectBag;
import core.Runtime;
import core.evaluables.method.NativeMethod;
import core.exceps.InterpretError;
import core.objs.ObjectDateTime;

import java.util.Calendar;

/**
* A method that extracts the time and stores it in a DateTime object.
* Created by Baltasar on 30/01/2016.
*/
public class NativeMethodOsNow extends NativeMethod {
public static final String EtqMthOsNow = "now";

public NativeMethodOsNow() {
super( EtqMthOsNow );
}

@Override
public ObjectBag doIt(ObjectBag ref, Evaluable[] params, StringBuilder msg)
throws InterpretError
{
final Calendar now = Calendar.getInstance();
final Runtime rt = Runtime.getRuntime();

chkParametersNumber( 0, params );

// Create date-time object
final ObjectDateTime toret = rt.createDateTime(
now.get( Calendar.DAY_OF_MONTH ),
now.get( Calendar.MONTH ) + 1,
now.get( Calendar.YEAR ),
now.get( Calendar.HOUR ),
now.get( Calendar.MINUTE ),
now.get( Calendar.SECOND )
);

msg.append( "Obtained current date and time" );
return toret;
}
}
43 changes: 43 additions & 0 deletions src/core/objs/ObjectDateTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package core.objs;

import core.ObjectBag;
import core.evaluables.method.InterpretedMethod;
import core.exceps.InterpretError;

/**
* Represents a time instant (date and time)
* Created by Baltasar on 29/01/2016.
*/
public class ObjectDateTime extends ValueObject {
public static final String EtqSecond = "second";
public static final String EtqMinute = "minute";
public static final String EtqHour = "hour";
public static final String EtqYear = "year";
public static final String EtqMonth = "month";
public static final String EtqDay = "day";
public static final String EtqDateAsStr = "dateAsStr";
public static final String EtqTimeAsStr = "timeAsStr";
public static final String EtqStr = "str";

public ObjectDateTime(String n, ObjectBag parent, ObjectBag container) throws InterpretError {
super( n, parent, container );

this.set( EtqDateAsStr, new InterpretedMethod( EtqDateAsStr,
"( ( ( ( self." + EtqDay + " str ) + \"-\" ) + ( self."
+ EtqMonth + " str ) ) + \"-\" ) + ( self."
+ EtqYear + " str )" ) );

this.set( EtqTimeAsStr, new InterpretedMethod( EtqTimeAsStr,
"( ( ( ( self." + EtqHour + " str ) + \":\" ) + ( self."
+ EtqMinute + " str ) ) + \":\" ) + ( self."
+ EtqSecond + " str )" ) );

this.set( EtqStr, new InterpretedMethod( EtqStr,
"( ( self dateAsStr ) + \" \" ) + ( self timeAsStr )" ) );
}

@Override
public Object getValueObject() {
return null;
}
}
27 changes: 27 additions & 0 deletions src/core/objs/ObjectOs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.objs;

import core.evaluables.method.InterpretedMethod;
import core.evaluables.method.nativemethods.NativeMethodOsNow;
import core.exceps.InterpretError;

/**
* The OS (Operating System) object which will provide services from the underlying
* system software layer.
* Created by Baltasar on 29/01/2016.
*/
public class ObjectOs extends SysObject {
public static final String Name = "os";

/**
* Creates the object representing the system
* When an empty name is passed, an automatic name is created.
*
* @param absParent The parent object in which to register it.
* @param container The object this is contained into.
*/
public ObjectOs(ObjectParent absParent, SysObject container) throws InterpretError {
super( Name, absParent, container );

this.set( NativeMethodOsNow.EtqMthOsNow, new NativeMethodOsNow() );
}
}

0 comments on commit c8ddaeb

Please sign in to comment.