2023-12-09 03:12:45 +00:00
|
|
|
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()
|
|
|
|
|
2023-12-11 00:14:13 +00:00
|
|
|
mainloop:
|
2023-12-09 03:12:45 +00:00
|
|
|
for {
|
|
|
|
tok, err := lx.Next()
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
// Reached EOF
|
|
|
|
// Terminate compilation
|
2023-12-11 00:14:13 +00:00
|
|
|
break mainloop
|
2023-12-09 03:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Real error
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("[line %d] %#v\n", lx.lineno, tok)
|
|
|
|
|
|
|
|
err = cc.Compile(tok)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-12-11 00:14:13 +00:00
|
|
|
|
|
|
|
err := cc.Finalize(dest)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-12-09 03:12:45 +00:00
|
|
|
}
|