You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.1 KiB
60 lines
1.1 KiB
package cmd
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"git.jfdev.de/JonasFranzDEV/hal/hal"
|
|
"git.jfdev.de/JonasFranzDEV/hal/parser"
|
|
)
|
|
|
|
func Execute() {
|
|
if err := rootCommand.Execute(); err != nil {
|
|
fmt.Printf("%v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
var rootCommand = &cobra.Command{
|
|
Args: cobra.ExactArgs(1),
|
|
Use: "hal",
|
|
RunE: runRootCommand,
|
|
}
|
|
|
|
var debug bool
|
|
|
|
func init() {
|
|
rootCommand.Flags().BoolVarP(&debug, "debug", "d", false, "Enable debug mode")
|
|
}
|
|
|
|
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)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
module, err := hal.NewHALModule(program, 256, 2, debug)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return module.Run()
|
|
}
|
|
|