static void Main(string[] args) { Neo.VM.ScriptBuilder builder = new Neo.VM.ScriptBuilder(); builder.Emit(Neo.VM.OpCode.NOP); builder.EmitPush(1); builder.EmitPush(2); builder.Emit(Neo.VM.OpCode.ADD); builder.Emit(Neo.VM.OpCode.RET); var machinecode = builder.ToArray(); var hexstr = "0x"; foreach (var m in machinecode) { hexstr += m.ToString("X02"); } Console.WriteLine("machinecode=" + hexstr); //run machinecode with neovm var engine = new Neo.VM.ExecutionEngine(); engine.LoadScript(machinecode); engine.Execute(); //show result var calcstack = engine.ResultStack; var v = calcstack.Peek(); Console.WriteLine("retvalue=" + v.GetBigInteger()); Console.ReadLine(); }
static void EmitCode(Neo.VM.ScriptBuilder builder, ISyntaxNode ast) { if (ast is SyntaxNode_Num) { builder.EmitPush((ast as SyntaxNode_Num).num); } else if (ast is SyntaxNode_Add) { foreach (var sn in ast.subnodes) { EmitCode(builder, sn); } builder.Emit(Neo.VM.OpCode.ADD); } }
static void Main(string[] args) { var srccode = " 1 + 2 + 4 + 5"; var ast = ParseSyntaxNode(srccode); DumpAST(ast); Neo.VM.ScriptBuilder builder = new Neo.VM.ScriptBuilder(); EmitCode(builder, ast); builder.Emit(Neo.VM.OpCode.RET); var machinecode = builder.ToArray(); var hexstr = "0x"; foreach (var m in machinecode) { hexstr += m.ToString("X02"); } Console.WriteLine("machinecode=" + hexstr); //run machinecode with neovm var engine = new Neo.VM.ExecutionEngine(); engine.LoadScript(machinecode); engine.Execute(); //show result var calcstack = engine.ResultStack; var v = calcstack.Peek(); Console.WriteLine("retvalue=" + v.GetBigInteger()); Console.ReadLine(); }