Move parser to seperate function

master
kolaente 5 years ago
parent 4875c536ec
commit 70c740cba2
Signed by: kolaente
GPG Key ID: F40E70337AB24C9B
  1. 19
      cmd/root.go
  2. 22
      parser/program_parser.go

@ -1,7 +1,6 @@
package cmd
import (
"bufio"
"fmt"
"os"
"time"
@ -42,23 +41,7 @@ func init() {
func runRootCommand(cmd *cobra.Command, args []string) error {
filename := args[0]
if stats, err := os.Stat(filename); err != nil || stats.IsDir() {
return err
}
file, err := os.Open(filename)
if err != nil {
return err
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var input []string
for scanner.Scan() {
input = append(input, scanner.Text())
}
if err := file.Close(); err != nil {
return err
}
program, err := parser.ParseProgram(input)
program, err := parser.ParseFile(filename)
if err != nil {
return err
}

@ -1,13 +1,35 @@
package parser
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"git.jfdev.de/JonasFranzDEV/hal/hal"
)
func ParseFile(filepath string) (hal.Program, error) {
if stats, err := os.Stat(filepath); err != nil || stats.IsDir() {
return nil, err
}
file, err := os.Open(filepath)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var input []string
for scanner.Scan() {
input = append(input, scanner.Text())
}
if err := file.Close(); err != nil {
return nil, err
}
return ParseProgram(input)
}
// ParseProgram parses an program by the following specification: LINE_NUMBER INSTRUCTION OPERAND(optional)
func ParseProgram(input []string) (hal.Program, error) {
program := make(hal.Program)

Loading…
Cancel
Save