|
|
|
@ -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) |
|
|
|
|