Skip to content

Commit

Permalink
Playgound: Added support for multiple statements in addition to one e…
Browse files Browse the repository at this point in the history
…xpression #19

Statements can be put on separate lines.
The return value of the last statement will become the evaluator result.
  • Loading branch information
FunctionPoint committed Oct 7, 2024
1 parent e6af131 commit 8b28dce
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
14 changes: 10 additions & 4 deletions Playground/src/Compiler/ClassCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,21 @@ export class ClassCompiler
this.method.vars.push( compiledVariable );
}

// Playground: Generates code to return the value of the last statement iso self.

compileStatements()
{
let lastNode: SourceNode | undefined;
while( !this.parser.atMethodEnd() ) {
let node = this.compileStatement();
this.method.body.add( node );
lastNode = this.compileStatement();
this.method.body.add( lastNode );
}

if( !this.method.hasReturn && !this.method.isConstructor() )
this.method.body.add( this.sourceNode( "\t\treturn this;\n", "return self" ) );
if( lastNode && !this.method.hasReturn )
lastNode.prepend( "return " );

// if( !this.method.hasReturn && !this.method.isConstructor() )
// this.method.body.add( this.sourceNode( "\t\treturn this;\n", "return self" ) );
}

// A statement consists of an expression,
Expand Down
3 changes: 2 additions & 1 deletion Playground/src/Evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ export class Evaluator
{
let stClass: string =
"CLASS Evaluator EXTENDS Object MODULE Evaluator CLASSVARS '' VARS ''\n" +
"METHODS\nevaluate\n\t^ " + stExpression + ".\n!\n";
// "METHODS\nevaluate\n\t^ " + stExpression + ".\n!\n";
"METHODS\nevaluate\n\t" + stExpression + "\n!\n";

let classCompiler = new ClassCompiler();
classCompiler.allClasses = this.loadCompiledClasses( compiledClassesJson );
Expand Down
3 changes: 2 additions & 1 deletion Playground/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<td>
<h1 class="field">Help</h1>
<div id="helpTitleDiv">
Enter any Smalltalk expression below and then press [>Evaluate].
Enter any Smalltalk expression or statements below and then press [>Evaluate].
For more help on the syntax of SmallJS, look at
<a href="https://github.com/Small-JS/SmallJS/blob/main/Smalltalk/Smalltalk.md"
target="_blank">Smalltalk.md</a>
Expand All @@ -41,6 +41,7 @@ <h1 class="field">Help</h1>
&nbsp 'Hello, ' , 'World' , $! toString<br>
&nbsp 1 + 2 < 4 ifTrue: [ 'smaller' ]<br>
&nbsp #( 1 2 3 ) map: [ :n | n squared ]<br>
&nbsp | a |&nbsp a := 4.&nbsp a squared.<br>
</div>
<p style="margin:5px;"></p>
</td>
Expand Down

0 comments on commit 8b28dce

Please sign in to comment.