Skip to content

Commit

Permalink
Merge pull request #75 from MoeOrganization/prakashk/explicit-coercion
Browse files Browse the repository at this point in the history
Explicit coercion and a few other minor changes
  • Loading branch information
Stevan Little committed Apr 3, 2013
2 parents 4cfa9ff + 81e4a15 commit df5c166
Show file tree
Hide file tree
Showing 15 changed files with 418 additions and 13 deletions.
2 changes: 1 addition & 1 deletion spec/syntax-examples/001-literals/002-float-literal/005.mo
@@ -1 +1 @@
.12345
0.12345
2 changes: 1 addition & 1 deletion spec/syntax-examples/001-literals/002-float-literal/006.mo
@@ -1 +1 @@
.5678e18
0.5678e18
21 changes: 15 additions & 6 deletions src/main/scala/org/moe/Moe.scala
Expand Up @@ -77,7 +77,12 @@ object Moe {

if (cmd.hasOption("e")) {
val code: String = cmd.getOptionValue("e")
REPL.evalLine(interpreter, runtime, code, Map("dumpAST" -> dumpAST))
REPL.evalLine(
interpreter,
runtime,
code,
Map("printOutput" -> false, "dumpAST" -> dumpAST, "printParserErrors" -> true)
)
return
}
else {
Expand All @@ -87,7 +92,7 @@ object Moe {
interpreter,
runtime,
Source.fromFile(path).mkString,
Map("printOutput" -> false, "dumpAST" -> dumpAST)
Map("printOutput" -> false, "dumpAST" -> dumpAST, "printParserErrors" -> true)
)

rest match {
Expand Down Expand Up @@ -170,20 +175,24 @@ object Moe {
val ast = CompilationUnitNode(
ScopeNode(nodes)
)
if (options("dumpAST")) {
if (options.contains("prettyPrintAST") && options("prettyPrintAST"))
if (options.getOrElse("dumpAST", false)) {
if (options.getOrElse("prettyPrintAST", false))
println(Serializer.toJSONPretty(ast))
else
println(Serializer.toJSON(ast))
}
val result = interpreter.eval(runtime, runtime.getRootEnv, ast)
if( options("printOutput") ) {
if( options.getOrElse("printOutput", false) ) {
println(result.toString)
}
EvalResult.Success
}
catch {
case i: MoeErrors.ParserInputIncomplete => EvalResult.Partial
case i: MoeErrors.ParserInputIncomplete => {
if (options.getOrElse("printParserErrors", false))
System.err.println(i)
EvalResult.Partial
}
case e: Exception => {
System.err.println(e)
EvalResult.Failure
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/org/moe/parser/MoeLiterals.scala
Expand Up @@ -7,6 +7,9 @@ import org.moe.ast._

trait MoeLiterals extends JavaTokenParsers {

// treat comments as whitespace
override val whiteSpace = """(#[^\n\r]*[\n\r]|\s)+""".r

// Numeric literals
def zeroNumber: Parser[IntLiteralNode] =
"""[\-\+]?0""".r ^^ { n => IntLiteralNode(0) }
Expand Down
15 changes: 13 additions & 2 deletions src/main/scala/org/moe/parser/MoeProductions.scala
Expand Up @@ -79,8 +79,19 @@ trait MoeProductions extends MoeLiterals with JavaTokenParsers with PackratParse

// This one is right-recursive (associative) instead of left
// right **
lazy val expOp: PackratParser[AST] = applyOp ~ "**" ~ expOp ^^ {
lazy val expOp: PackratParser[AST] = coerceOp ~ "**" ~ expOp ^^ {
case left ~ op ~ right => BinaryOpNode(left, op, right)
} | coerceOp

// Symbolic unary -- left + (num), ? (bool), . (str)
// used for explicit coercion
// (see: http://perlcabal.org/syn/S03.html#Symbolic_unary_precedence)

// Perl6 uses ~ for stringification (same as its concatentation op);
// since our concat op is ".", we use it as the prefix op

lazy val coerceOp: PackratParser[AST] = "[+?.]".r ~ applyOp ^^ {
case op ~ expr => PrefixUnaryOpNode(expr, op)
} | applyOp

// TODO: nonassoc ++ --
Expand Down Expand Up @@ -252,7 +263,7 @@ trait MoeProductions extends MoeLiterals with JavaTokenParsers with PackratParse
*/

def statementDelim: Parser[List[String]] = rep1(";")
def statements: Parser[StatementsNode] = repsep(statement, statementDelim) ^^ StatementsNode
def statements: Parser[StatementsNode] = repsep(statement, statementDelim) <~ statementDelim.? ^^ StatementsNode

def blockContent: Parser[StatementsNode] = statements <~ statementDelim.?
def block: Parser[StatementsNode] = "{" ~> blockContent <~ "}"
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/org/moe/runtime/MoeErrors.scala
Expand Up @@ -52,4 +52,7 @@ object MoeErrors {
// Parser errors
class ParserInputIncomplete (msg: String) extends MoeException(msg)
class ParserInputError (msg: String) extends MoeException(msg)

// Coerce errors
class CannotCoerceError (msg: String) extends MoeException(msg)
}
58 changes: 57 additions & 1 deletion src/main/scala/org/moe/runtime/builtins/BoolClass.scala
Expand Up @@ -24,12 +24,68 @@ object BoolClass {

import r.NativeObjects._

// coercion

boolClass.addMethod(
new MoeMethod(
"Int",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeIntContext()))
)
)

boolClass.addMethod(
new MoeMethod(
"prefix:<+>",
new MoeSignature(),
env,
(e) => getInt(self(e).unboxToInt.getOrElse(0))
(e) => self(e).coerce(r, Some(MoeIntContext()))
)
)

boolClass.addMethod(
new MoeMethod(
"Num",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeNumContext()))
)
)

boolClass.addMethod(
new MoeMethod(
"Bool",
new MoeSignature(),
env,
(e) => self(e)
)
)

boolClass.addMethod(
new MoeMethod(
"prefix:<?>",
new MoeSignature(),
env,
(e) => self(e)
)
)

boolClass.addMethod(
new MoeMethod(
"Str",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeStrContext()))
)
)

boolClass.addMethod(
new MoeMethod(
"prefix:<.>",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeStrContext()))
)
)

Expand Down
65 changes: 65 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/IntClass.scala
Expand Up @@ -353,6 +353,71 @@ object IntClass {
)
)

// coercion

intClass.addMethod(
new MoeMethod(
"Int",
new MoeSignature(),
env,
(e) => self(e)
)
)

intClass.addMethod(
new MoeMethod(
"prefix:<+>",
new MoeSignature(),
env,
(e) => self(e)
)
)

intClass.addMethod(
new MoeMethod(
"Num",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeNumContext()))
)
)

intClass.addMethod(
new MoeMethod(
"Bool",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeBoolContext()))
)
)

intClass.addMethod(
new MoeMethod(
"prefix:<?>",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeBoolContext()))
)
)

intClass.addMethod(
new MoeMethod(
"Str",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeStrContext()))
)
)

intClass.addMethod(
new MoeMethod(
"prefix:<.>",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeStrContext()))
)
)

/**
* List of Operators to support:
* - prefix:<->
Expand Down
65 changes: 65 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/NumClass.scala
Expand Up @@ -191,6 +191,71 @@ object NumClass {
)
)

// coercion

numClass.addMethod(
new MoeMethod(
"Int",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeIntContext()))
)
)

numClass.addMethod(
new MoeMethod(
"Num",
new MoeSignature(),
env,
(e) => self(e)
)
)

numClass.addMethod(
new MoeMethod(
"prefix:<+>",
new MoeSignature(),
env,
(e) => self(e)
)
)

numClass.addMethod(
new MoeMethod(
"Bool",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeBoolContext()))
)
)

numClass.addMethod(
new MoeMethod(
"prefix:<?>",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeBoolContext()))
)
)

numClass.addMethod(
new MoeMethod(
"Str",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeStrContext()))
)
)

numClass.addMethod(
new MoeMethod(
"prefix:<.>",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeStrContext()))
)
)

/**
* List of Operators to support:
* - prefix:<->
Expand Down
65 changes: 65 additions & 0 deletions src/main/scala/org/moe/runtime/builtins/StrClass.scala
Expand Up @@ -238,6 +238,71 @@ object StrClass {
)
)

// coercion

strClass.addMethod(
new MoeMethod(
"Str",
new MoeSignature(),
env,
(e) => self(e)
)
)

strClass.addMethod(
new MoeMethod(
"prefix:<.>",
new MoeSignature(),
env,
(e) => self(e)
)
)

strClass.addMethod(
new MoeMethod(
"Int",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeIntContext()))
)
)

strClass.addMethod(
new MoeMethod(
"Num",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeNumContext()))
)
)

strClass.addMethod(
new MoeMethod(
"prefix:<+>",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeNumContext()))
)
)

strClass.addMethod(
new MoeMethod(
"Bool",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeBoolContext()))
)
)

strClass.addMethod(
new MoeMethod(
"prefix:<?>",
new MoeSignature(),
env,
(e) => self(e).coerce(r, Some(MoeBoolContext()))
)
)

/**
* List of Operators to support:
* - infix:<.>
Expand Down

0 comments on commit df5c166

Please sign in to comment.