Skip to content

Instantly share code, notes, and snippets.

@MaximTrushin
Created May 6, 2011 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaximTrushin/959350 to your computer and use it in GitHub Desktop.
Save MaximTrushin/959350 to your computer and use it in GitHub Desktop.
Hot to compile Shakespeare. Boo.OMeta.Parser example
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