Created
May 6, 2011 17:10
-
-
Save MaximTrushin/959350 to your computer and use it in GitHub Desktop.
Hot to compile Shakespeare. Boo.OMeta.Parser example
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
namespace Vincent.Tests | |
import System | |
import Boo.OMeta | |
import Boo.OMeta.Parser | |
import Boo.Lang.Compiler.Ast | |
import Boo.Lang.Compiler.Steps | |
import Boo.Lang.Compiler.IO | |
import System.IO | |
def newShakespeareAssignment(l,r): | |
return BinaryExpression(Operator: BinaryOperatorType.Assign, Left: l, Right: newStringLiteral(flatString(r))) | |
syntax Shakespeare: | |
assignment = shakespeare_assignment | super | |
shakespeare_assignment = (++(~(tit),_) >> r, tit, reference >> l, ("."|"") ^ newShakespeareAssignment(l,r) ) | |
tit = --("," | ":"| " "), "that is the" | |
def OMetaParseAndRun(code as string): | |
compiler = Boo.Lang.Compiler.BooCompiler() | |
compiler.Parameters.OutputWriter = StringWriter() | |
compiler.Parameters.References.Add(typeof(Shakespeare).Assembly) | |
compiler.Parameters.Pipeline = Boo.Lang.Compiler.Pipelines.CompileToMemory() | |
compiler.Parameters.Pipeline.Add(RunAssembly()) | |
compiler.Parameters.Pipeline.Replace(typeof(Parsing), Boo.OMeta.Parser.BooParserStep()) | |
compiler.Parameters.Input.Add(StringInput("", code)) | |
return compiler.Run() | |
code = """ | |
To be, or not to be, that is the question. | |
print "\$question" | |
""" | |
output = StringWriter() | |
Console.SetOut(output) | |
OMetaParseAndRun(code) | |
assert output.ToString() == "To be, or not to be\r\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment