func (b *build) Execute(args []string) error { if len(args) != 1 { return fmt.Errorf("expected an input file") } inf, err := os.Open(args[0]) if err != nil { return err } defer inf.Close() c := new(compiler.Compiler) f, err := c.Compile(args[0], inf) if err != nil { return err } out := stdout if b.Output != "" { outf, err := os.Create(b.Output) if err != nil { return err } defer outf.Close() out = outf } if b.Asm { dasm := new(compiler.Disasm) err = dasm.ToAsm(f, out) } else { enc := bytecode.NewEncoder(out) err = enc.Encode(f) } if err != nil { return err } return nil }
// Execute the assembler command func (a *asm) Execute(args []string) error { // Validate input if len(args) != 1 { return fmt.Errorf("expected an input file name") } // Open input file inf, err := os.Open(args[0]) if err != nil { return err } defer inf.Close() // Compile to bytecode File f, err := new(compiler.Asm).Compile(args[0], inf) if err != nil { return err } // Write output var out io.Writer out = stdout if a.Output != "" { outF, err := os.Create(a.Output) if err != nil { return err } defer outF.Close() out = outF } // Encode to bytecode buf := bytes.NewBuffer(nil) err = bytecode.NewEncoder(buf).Encode(f) if err != nil { return err } if a.Hexa { _, err = io.WriteString(out, fmt.Sprintf("%x", buf.Bytes())) } else { _, err = out.Write(buf.Bytes()) } if err != nil { return err } return nil }
func TestAsm(t *testing.T) { a := new(Asm) for i, c := range asmcases { if isolateAsmCase >= 0 && isolateAsmCase != i { continue } if testing.Verbose() { fmt.Printf("testing assembler case %d...\n", i) } // Act var got []byte f, err := a.Compile(c.id, strings.NewReader(c.src)) if err == nil { buf := bytes.NewBuffer(nil) err = bytecode.NewEncoder(buf).Encode(f) got = buf.Bytes() } // Assert if err != c.err { if c.err == nil { t.Errorf("[%d] - expected no error, got `%s`", i, err) } else { t.Errorf("[%d] - expected error `%s`, got `%s`", i, c.err, err) } } if c.exp != nil { if bytes.Compare(got, c.exp) != 0 { t.Errorf("[%d] - expected \n%x\n, got \n%x\n", i, c.exp, got) } } if c.err == nil && c.exp == nil { t.Errorf("[%d] - no assertion", i) } } }