-
Notifications
You must be signed in to change notification settings - Fork 0
/
lmc.go
143 lines (127 loc) · 3.01 KB
/
lmc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"github.com/urfave/cli"
"os"
"log"
"net/url"
"fmt"
"github.com/jamiemansfield/lmcemu/asm"
"github.com/mileusna/conditional"
"encoding/json"
"github.com/jamiemansfield/lmcemu/emu"
"github.com/jamiemansfield/lmcemu/programs"
"strings"
)
var (
inputFlag = cli.StringFlag{
Name: "input, i",
Usage: "the input file or URI",
}
)
type lmcFunc func(instructions []*asm.Instruction) error
func createAction(lmcFunc lmcFunc) cli.ActionFunc {
return func(c *cli.Context) error {
input := c.String("input")
uri, err := url.Parse(input)
if err != nil {
return err
}
var instructions []*asm.Instruction
if uri.Scheme == "file" || uri.Scheme == "" {
file, err := os.Open(conditional.String(uri.Scheme == "", uri.Path, uri.Opaque))
if err != nil {
return err
}
defer file.Close()
// Create a Parser and parse the file
parser := asm.CreateParser()
parser.ReadFromFile(file)
// Assemble the program.
insts, err := parser.Assemble()
if err != nil {
return err
}
instructions = insts
} else
if uri.Scheme == "builtin" {
insts := programs.BuiltinRegistry[strings.ToLower(uri.Opaque)]
if insts == nil {
return fmt.Errorf("cli: a builtin program of name '%s' does not exist", uri.Opaque)
}
instructions = insts
} else {
return fmt.Errorf("cli: unsupported URI scheme ('%s') used", uri.Scheme)
}
return lmcFunc(instructions)
}
}
func main() {
app := cli.NewApp()
app.Name = "lmcemu"
app.Usage = "a Little Man Computer emulator"
app.Version = "1.0.0-indev"
app.Commands = []cli.Command{
{
Name: "compile",
Aliases: []string{"c"},
Usage: "compiles the given assembly file to machine code",
Flags: []cli.Flag {
inputFlag,
},
Action: createAction(func (instructions []*asm.Instruction) error {
// Assemble the program into machine code
prog, err := asm.AssembleProgram(instructions)
if err != nil {
return err
}
// Print the machine code
fmt.Printf("%v", prog)
return nil
}),
},
{
Name: "dump",
Aliases: []string{"d"},
Usage: "dumps the given assembly file as a JSON output",
Flags: []cli.Flag {
inputFlag,
},
Action: createAction(func (instructions []*asm.Instruction) error {
// Dump in JSON form
bytes, err := json.MarshalIndent(instructions, "", "\t")
if err != nil {
return err
}
fmt.Println(string(bytes))
return nil
}),
},
{
Name: "run",
Aliases: []string{"r"},
Usage: "runs the given assembly file",
Flags: []cli.Flag {
inputFlag,
},
Action: createAction(func (instructions []*asm.Instruction) error {
// Assemble the program into machine code
prog, err := asm.AssembleProgram(instructions)
if err != nil {
return err
}
// Run
cpu := emu.CreateLmcCpu()
memory := emu.CreateMemory(prog)
err = cpu.Execute(memory)
if err != nil {
return err
}
return nil
}),
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}