diff --git a/cmd/casmeleon/AssemblyStream.go b/cmd/casmeleon/AssemblyStream.go index 18ed0c6..fd222d0 100644 --- a/cmd/casmeleon/AssemblyStream.go +++ b/cmd/casmeleon/AssemblyStream.go @@ -10,7 +10,7 @@ import ( "github.com/aleferri/casmeleon/pkg/text" ) -//IdentifySymbol in the casm language +// IdentifySymbol in the casm language func IdentifySymbol(s []rune, fileOffset uint32, count uint32) text.Symbol { if unicode.IsDigit(s[0]) { return text.SymbolOf(fileOffset, count, string(s), text.Number) @@ -38,7 +38,7 @@ func IdentifySymbol(s []rune, fileOffset uint32, count uint32) text.Symbol { return text.SymbolOf(fileOffset, count, str, id) } -//AssemblyStream is the stream of the symbols +// AssemblyStream is the stream of the symbols type AssemblyStream struct { parent *AssemblyStream source *bufio.Reader @@ -46,7 +46,7 @@ type AssemblyStream struct { repo *text.Source } -//MakeRootStream for the parser +// MakeRootStream for the parser func MakeRootStream(source *bufio.Reader, repo *text.Source) parser.Stream { return &AssemblyStream{parent: nil, source: source, repo: repo, buffer: []text.Symbol{}} } @@ -55,11 +55,16 @@ func MakeChildStream(source *bufio.Reader, repo *text.Source, parent *AssemblySt return &AssemblyStream{parent: parent, source: source, repo: repo, buffer: []text.Symbol{}} } -//Buffer ensure that the buffer of the stream contains at least 1 element +// Buffer ensure that the buffer of the stream contains at least 1 element func (s *AssemblyStream) Buffer() { for len(s.buffer) == 0 { line, ioErr := s.source.ReadBytes('\n') + if ioErr != nil { + s.buffer = append(s.buffer, text.SymbolEmpty(s.repo.FileIndex()).WithID(text.EOF)) + return + } + runes := bytes.Runes(line) temps, _ := scanner.FastScan(runes, true, scanFollowMap) @@ -74,14 +79,10 @@ func (s *AssemblyStream) Buffer() { s.buffer = append(s.buffer, sym) } } - - if ioErr != nil { - s.buffer = append(s.buffer, text.SymbolEmpty(s.repo.FileIndex()).WithID(text.EOF)) - } } } -//Next symbol in the internal buffer +// Next symbol in the internal buffer func (s *AssemblyStream) Next() text.Symbol { s.Buffer() @@ -90,14 +91,14 @@ func (s *AssemblyStream) Next() text.Symbol { return result } -//Peek the symbol from the internal buffer +// Peek the symbol from the internal buffer func (s *AssemblyStream) Peek() text.Symbol { s.Buffer() return s.buffer[0] } -//Source of the stream +// Source of the stream func (s *AssemblyStream) Source() *text.Source { return s.repo } diff --git a/cmd/casmeleon/Main.go b/cmd/casmeleon/Main.go index 1e6545d..59e650d 100755 --- a/cmd/casmeleon/Main.go +++ b/cmd/casmeleon/Main.go @@ -119,6 +119,7 @@ func ParseASMFile(lang casm.Language, sourceFile string) (*AssemblyProgram, erro } return nil, errors.New("Error during compilation") } + parser.ConsumeAll(stream, text.EOL) } @@ -137,7 +138,10 @@ func main() { flag.StringVar(&langFileName, "lang", ".", "-lang=langfile") var debugMode bool flag.BoolVar(&debugMode, "debug", false, "-debug=true|false") + var exportAssembly string + flag.StringVar(&exportAssembly, "export", "bin", "-export=bin|hex") flag.Parse() + tUI := ui.NewConsole(false, false) if strings.EqualFold(langFileName, ".") { tUI.ReportError("missing -lang=langfile", true) @@ -179,8 +183,16 @@ func main() { for _, f := range flag.Args() { if !strings.HasPrefix(f, "-") { + if debugMode { + fmt.Println("Parsing " + f) + } + program, errAsm := ParseASMFile(lang, f) + if debugMode { + fmt.Println("End parsing") + } + if errAsm != nil { fmt.Println(errAsm.Error()) break diff --git a/pkg/parser/Match.go b/pkg/parser/Match.go index 09d823a..77fb648 100644 --- a/pkg/parser/Match.go +++ b/pkg/parser/Match.go @@ -4,10 +4,10 @@ import ( "github.com/aleferri/casmeleon/pkg/text" ) -//Match a generic rule in the provided stream +// Match a generic rule in the provided stream type Match func(stream Stream) (CSTNode, error) -//Accept symbol of type sym +// Accept symbol of type sym func Accept(stream Stream, sym uint32) (text.Symbol, bool) { v := stream.Peek() if v.ID() == sym { @@ -16,13 +16,13 @@ func Accept(stream Stream, sym uint32) (text.Symbol, bool) { return v, false } -//Consume symbol of type sym +// Consume symbol of type sym func Consume(stream Stream, sym uint32) bool { _, c := Accept(stream, sym) return c } -//ConsumeAll symbols of type sym +// ConsumeAll symbols of type sym func ConsumeAll(stream Stream, sym uint32) int { i := 0 for Consume(stream, sym) { @@ -31,7 +31,7 @@ func ConsumeAll(stream Stream, sym uint32) int { return i } -//Require accept a symbol of type sym or return an error if the symbol is not the accepted symbol type +// Require accept a symbol of type sym or return an error if the symbol is not the accepted symbol type func Require(stream Stream, sym uint32) (text.Symbol, error) { v, c := Accept(stream, sym) if c { @@ -40,13 +40,13 @@ func Require(stream Stream, sym uint32) (text.Symbol, error) { return v, ExpectedSymbol(v, "Unexpected '%s', was expecting: %s", sym) } -//Expect consume a symbol of type sym or return an error if the symbol is not the accepted symbol type +// Expect consume a symbol of type sym or return an error if the symbol is not the accepted symbol type func Expect(stream Stream, sym uint32) error { _, e := Require(stream, sym) return e } -//AcceptAny accept any of the proposed symbols +// AcceptAny accept any of the proposed symbols func AcceptAny(stream Stream, syms ...uint32) (text.Symbol, bool) { peek := stream.Peek() for _, sym := range syms { @@ -57,13 +57,13 @@ func AcceptAny(stream Stream, syms ...uint32) (text.Symbol, bool) { return peek, false } -//ConsumeAny symbol of type syms +// ConsumeAny symbol of type syms func ConsumeAny(stream Stream, syms ...uint32) bool { _, c := AcceptAny(stream, syms...) return c } -//RequireAny of the listed symbols +// RequireAny of the listed symbols func RequireAny(stream Stream, syms ...uint32) (text.Symbol, error) { v, c := AcceptAny(stream, syms...) if !c { @@ -72,13 +72,13 @@ func RequireAny(stream Stream, syms ...uint32) (text.Symbol, error) { return v, nil } -//ExpectAny of the listed symbols +// ExpectAny of the listed symbols func ExpectAny(stream Stream, syms ...uint32) error { _, err := RequireAny(stream, syms...) return err } -//RequireSequence specified by caller +// RequireSequence specified by caller func RequireSequence(stream Stream, seq ...uint32) ([]text.Symbol, error) { acc := []text.Symbol{} for _, m := range seq { @@ -91,7 +91,7 @@ func RequireSequence(stream Stream, seq ...uint32) ([]text.Symbol, error) { return acc, nil } -//AcceptInsetPattern in a stream +// AcceptInsetPattern in a stream func AcceptInsetPattern(stream Stream, left uint32, right uint32, seq ...uint32) ([]text.Symbol, error) { acc := []text.Symbol{} matchLeft := Expect(stream, left) @@ -111,9 +111,9 @@ func AcceptInsetPattern(stream Stream, left uint32, right uint32, seq ...uint32) return acc, matchRight } -//AcceptInsetDelegate read left expected symbol, then test for the right expected symbol. -//If symbol is not the specified symbol this function will delegate the inset matching on the provided function -//In the end the right symbol is read +// AcceptInsetDelegate read left expected symbol, then test for the right expected symbol. +// If symbol is not the specified symbol this function will delegate the inset matching on the provided function +// In the end the right symbol is read func AcceptInsetDelegate(stream Stream, left uint32, right uint32, dg Match) ([]CSTNode, error) { leafs := []CSTNode{} matchLeft := Expect(stream, left) @@ -131,7 +131,7 @@ func AcceptInsetDelegate(stream Stream, left uint32, right uint32, dg Match) ([] return leafs, matchRight } -//AcceptPatternWithTest in the stream source +// AcceptPatternWithTest in the stream source func AcceptPatternWithTest(stream Stream, left uint32, right uint32, test uint32, dg Match) ([]CSTNode, error) { leafs := []CSTNode{} matchLeft := Expect(stream, left)