58 lines
734 B
Go
58 lines
734 B
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
)
|
|
|
|
//
|
|
|
|
func main() {
|
|
src := flag.String("Src", "", "Input file")
|
|
flag.Parse()
|
|
|
|
fh, err := os.OpenFile(*src, os.O_RDONLY, 0644)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
assemble(fh, ioutil.Discard)
|
|
}
|
|
|
|
func assemble(src io.Reader, dest io.Writer) {
|
|
lx := NewLexer(src)
|
|
cc := NewCompiler()
|
|
|
|
mainloop:
|
|
for {
|
|
tok, err := lx.Next()
|
|
if err != nil {
|
|
if errors.Is(err, io.EOF) {
|
|
// Reached EOF
|
|
// Terminate compilation
|
|
break mainloop
|
|
}
|
|
|
|
// Real error
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Printf("[line %d] %#v\n", lx.lineno, tok)
|
|
|
|
err = cc.Compile(tok)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
}
|
|
|
|
err := cc.Finalize(dest)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|