Skip to content

Commit

Permalink
feat: add main method
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed Mar 29, 2022
1 parent e7304e2 commit 02cbb26
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions cmd/brainfuck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright © 2021 Rak Laptudirm <raklaptudirm@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"os"

"laptudirm.com/x/brainfuck/pkg/lexer"
"laptudirm.com/x/brainfuck/pkg/parser"
"laptudirm.com/x/brainfuck/pkg/token"
"laptudirm.com/x/brainfuck/pkg/vm"
)

const Memory = 30000

func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "usage: brainfuck [filename]")
os.Exit(1)
}

filename := os.Args[1]
file, err := os.ReadFile(filename)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}

var l lexer.Lexer
l.Init(string(file), handleErr)

var p parser.Parser
p.Init(&l, handleErr)

program := p.ParseProgram()

if l.ErrorCount > 0 || p.ErrorCount > 0 {
os.Exit(1)
}

m := vm.New(Memory)
err = m.Execute(program)
if err != nil {
printErr(err.Error())
os.Exit(1)
}
}

func handleErr(p token.Position, e string) {
printErr("%s: %v\n", p, e)
}

func printErr(format string, a ...interface{}) error {
_, err := fmt.Fprintf(os.Stderr, format, a...)
return err
}

0 comments on commit 02cbb26

Please sign in to comment.