Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added variables and assignment, as well as blocks with multiple state…
…ments.
- Loading branch information
zzorn
committed
May 25, 2012
1 parent
61cee10
commit e1cc700
Showing
10 changed files
with
294 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.shapefun.parser.syntaxtree | ||
|
||
import org.shapefun.parser.Context | ||
|
||
/** | ||
* | ||
*/ | ||
case class Block(statements: List[SyntaxNode]) extends Expr { | ||
|
||
def returnType(): Class[_] = if (statements.isEmpty) classOf[Unit] else statements.last.returnType() | ||
|
||
def checkTypes() { | ||
statements foreach {_.checkTypes()} | ||
} | ||
|
||
def calculate(context: Context): AnyRef = { | ||
var result: AnyRef = Unit | ||
statements foreach {s => result = s.calculate(context)} | ||
result | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/scala/org/shapefun/parser/syntaxtree/IncDecOp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.shapefun.parser.syntaxtree | ||
|
||
import org.shapefun.parser.Context | ||
import org.shapefun.utils.ParameterChecker | ||
|
||
/** | ||
* | ||
*/ | ||
case class IncDecOp(identifier: Symbol, increment: Boolean) extends Expr { | ||
ParameterChecker.requireIsIdentifier(identifier, 'identifier) | ||
|
||
def checkTypes() { | ||
// TODO: Get variable type from type checking context, check that it is numeric | ||
} | ||
|
||
def returnType(): Class[_] = { | ||
// TODO: Get variable type from type checking context | ||
null | ||
} | ||
|
||
def calculate(context: Context): AnyRef = { | ||
val oldBoxedVal: AnyRef = context.getVariable(identifier) | ||
val oldValue = Double.unbox(oldBoxedVal) | ||
val newValue = Double.box(if (increment) oldValue + 1 else oldValue - 1) | ||
context.setVariable(identifier, newValue) | ||
oldBoxedVal | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/scala/org/shapefun/parser/syntaxtree/SyntaxNode.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.shapefun.parser.syntaxtree | ||
|
||
import org.shapefun.parser.Context | ||
|
||
/** | ||
* | ||
*/ | ||
trait SyntaxNode { | ||
|
||
def checkTypes() | ||
|
||
def returnType(): Class[_] | ||
|
||
def calculate(context: Context): AnyRef | ||
|
||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/scala/org/shapefun/parser/syntaxtree/VarAssignment.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.shapefun.parser.syntaxtree | ||
|
||
import org.shapefun.parser.Context | ||
import org.shapefun.utils.ParameterChecker | ||
|
||
/** | ||
* | ||
*/ | ||
case class VarAssignment(identifier: Symbol, assignmentOp: Symbol, valueExpr: Expr) extends SyntaxNode { | ||
ParameterChecker.requireIsIdentifier(identifier, 'identifier) | ||
|
||
def checkTypes() { | ||
valueExpr.checkTypes() | ||
// TODO: Get variable type from type checking context,check that value matches | ||
} | ||
|
||
def returnType(): Class[_] = { | ||
// TODO: Get variable type from type checking context | ||
null | ||
} | ||
|
||
def calculate(context: Context): AnyRef = { | ||
|
||
val value = valueExpr.calculate(context) | ||
|
||
val newValue = if (assignmentOp == '=) value | ||
else { | ||
val oldValue = Double.unbox(context.getVariable(identifier)) | ||
val numValue = Double.unbox(value) | ||
Double.box(assignmentOp match { | ||
case '+= => oldValue + numValue | ||
case '-= => oldValue - numValue | ||
case '*= => oldValue * numValue | ||
case '/= => oldValue / numValue | ||
}) | ||
} | ||
|
||
context.setVariable(identifier, newValue) | ||
|
||
newValue | ||
} | ||
} |
Oops, something went wrong.