@@ -633,7 +633,59 @@ class GSWasmVisitor extends WatParserBaseVisitor[WIR] {
633633 else if (ctx.MEMORY != null ) ExportMemory (id)
634634 else if (ctx.GLOBAL != null ) ExportGlobal (id)
635635 else error
636+ }
637+
638+ override def visitScriptModule (ctx : ScriptModuleContext ): Module = {
639+ if (ctx.module_ != null ) {
640+ visitModule_(ctx.module_).asInstanceOf [Module ]
641+ } else {
642+ throw new RuntimeException (" Unsupported" )
643+ }
644+ }
645+
646+ override def visitAction_ (ctx : Action_Context ): Action = {
647+ if (ctx.INVOKE != null ) {
648+ val instName = if (ctx.VAR != null ) Some (ctx.VAR ().getText) else None
649+ var name = ctx.name.getText.substring(1 ).dropRight(1 )
650+ var args = for (constCtx <- ctx.constList.wconst.asScala) yield {
651+ val Array (ty, _) = constCtx.CONST .getText.split(" \\ ." )
652+ visitLiteralWithType(constCtx.literal, toNumType(ty))
653+ }
654+ Invoke (instName, name, args.toList)
655+ } else {
656+ throw new RuntimeException (" Unsupported" )
657+ }
658+ }
659+
660+ override def visitAssertion (ctx : AssertionContext ): Assertion = {
661+ if (ctx.ASSERT_RETURN != null ) {
662+ val action = visitAction_(ctx.action_)
663+ val expect = for (constCtx <- ctx.constList.wconst.asScala) yield {
664+ val Array (ty, _) = constCtx.CONST .getText.split(" \\ ." )
665+ visitLiteralWithType(constCtx.literal, toNumType(ty))
666+ }
667+ println(s " expect = $expect" )
668+ AssertReturn (action, expect.toList)
669+ } else {
670+ throw new RuntimeException (" Unsupported" )
671+ }
672+ }
636673
674+ override def visitCmd (ctx : CmdContext ): Cmd = {
675+ if (ctx.assertion != null ) {
676+ visitAssertion(ctx.assertion)
677+ } else if (ctx.scriptModule != null ) {
678+ CmdModule (visitScriptModule(ctx.scriptModule))
679+ } else {
680+ throw new RuntimeException (" Unsupported" )
681+ }
682+ }
683+
684+ override def visitScript (ctx : ScriptContext ): WIR = {
685+ val cmds = for (cmd <- ctx.cmd.asScala) yield {
686+ visitCmd(cmd)
687+ }
688+ Script (cmds.toList)
637689 }
638690
639691 override def visitTag (ctx : TagContext ): WIR = {
@@ -645,15 +697,35 @@ class GSWasmVisitor extends WatParserBaseVisitor[WIR] {
645697}
646698
647699object Parser {
648- def parse (input : String ): Module = {
700+ private def makeWatVisitor (input : String ) = {
649701 val charStream = new ANTLRInputStream (input)
650702 val lexer = new WatLexer (charStream)
651703 val tokens = new CommonTokenStream (lexer)
652- val parser = new WatParser (tokens)
704+ new WatParser (tokens)
705+ }
706+
707+ def parse (input : String ): Module = {
708+ val parser = makeWatVisitor(input)
653709 val visitor = new GSWasmVisitor ()
654710 val res : Module = visitor.visit(parser.module).asInstanceOf [Module ]
655711 res
656712 }
657713
658714 def parseFile (filepath : String ): Module = parse(scala.io.Source .fromFile(filepath).mkString)
715+
716+ // parse extended webassembly script language
717+ def parseScript (input : String ): Option [Script ] = {
718+ val parser = makeWatVisitor(input)
719+ val visitor = new GSWasmVisitor ()
720+ val tree = parser.script()
721+ val errorNumer = parser.getNumberOfSyntaxErrors()
722+ if (errorNumer != 0 ) None
723+ else {
724+ val res : Script = visitor.visitScript(tree).asInstanceOf [Script ]
725+ Some (res)
726+ }
727+ }
728+
729+ def parseScriptFile (filepath : String ): Option [Script ] =
730+ parseScript(scala.io.Source .fromFile(filepath).mkString)
659731}
0 commit comments