Skip to content

Commit 6dbbcc0

Browse files
committed
Standardize errors, remove unused code.
1 parent 562e83a commit 6dbbcc0

File tree

8 files changed

+37
-118
lines changed

8 files changed

+37
-118
lines changed

pkg/forth/cell.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type CellNumber struct {
3737
}
3838

3939
func (c CellNumber) Execute(vm *VirtualMachine) error {
40-
return fmt.Errorf("Cannot directly execute a number")
40+
return fmt.Errorf("cannot directly execute a number")
4141
}
4242

4343
func (c CellNumber) String() string {
@@ -52,7 +52,7 @@ func (c CellNumber) AddToList(u *Ulp) error {
5252
}
5353

5454
func (c CellNumber) BuildExecution(u *Ulp) (string, error) {
55-
return "", fmt.Errorf("Cannot directly execute number")
55+
return "", fmt.Errorf("cannot directly execute number")
5656
}
5757

5858
func (c CellNumber) OutputReference(u *Ulp) (string, error) {
@@ -75,19 +75,19 @@ func (c CellAddress) Execute(vm *VirtualMachine) error {
7575
switch w := c.Entry.Word.(type) {
7676
case *WordForth:
7777
if c.Offset >= len(w.Cells) {
78-
return fmt.Errorf("Trying to get data from outside of allocated data.")
78+
return fmt.Errorf("trying to get data from outside of allocated data")
7979
}
8080
if c.Entry.Flag.Data {
8181
return vm.Stack.Push(w.Cells[c.Offset])
8282
}
8383
return w.ExecuteOffset(vm, c.Offset)
8484
case *WordPrimitive:
8585
if c.Offset != 0 {
86-
return fmt.Errorf("Cannot execute a primitive word at an offset.")
86+
return fmt.Errorf("cannot execute a primitive word at an offset")
8787
}
8888
return w.Execute(vm)
8989
default:
90-
return fmt.Errorf("Cannot execute word type %t", c.Entry.Word)
90+
return fmt.Errorf("cannot execute word type %t", c.Entry.Word)
9191
}
9292
}
9393

@@ -108,7 +108,7 @@ func (c CellAddress) BuildExecution(u *Ulp) (string, error) {
108108
case UlpCompileTargetSubroutine:
109109
return fmt.Sprintf("jump %s", name), nil
110110
default:
111-
return "", fmt.Errorf("Unknown compile target %d, please file a bug report", u.compileTarget)
111+
return "", fmt.Errorf("unknown compile target %d, please file a bug report", u.compileTarget)
112112
}
113113
}
114114

@@ -200,12 +200,12 @@ func (c CellLiteral) BuildExecution(u *Ulp) (string, error) {
200200
case UlpCompileTargetSubroutine:
201201
return fmt.Sprintf("move r0, %s\r\n%sjump __add_to_stack", name, safeCall()), nil
202202
default:
203-
return "", fmt.Errorf("Unknown compile target %d, please file a bug report", u.compileTarget)
203+
return "", fmt.Errorf("unknown compile target %d, please file a bug report", u.compileTarget)
204204
}
205205
}
206206

207207
func (c CellLiteral) OutputReference(u *Ulp) (string, error) {
208-
return "", fmt.Errorf("Cannot refer to a CellLiteral, please file a bug report")
208+
return "", fmt.Errorf("cannot refer to a CellLiteral, please file a bug report")
209209
}
210210

211211
func (c CellLiteral) IsRecursive(check *WordForth) bool {
@@ -232,7 +232,7 @@ func (c *CellDestination) BuildExecution(u *Ulp) (string, error) {
232232
}
233233

234234
func (c *CellDestination) OutputReference(u *Ulp) (string, error) {
235-
return "", fmt.Errorf("Cannot refer to a destination, please file a bug report")
235+
return "", fmt.Errorf("cannot refer to a destination, please file a bug report")
236236
}
237237

238238
func (c *CellDestination) IsRecursive(check *WordForth) bool {
@@ -281,12 +281,12 @@ func (c *CellBranch) BuildExecution(u *Ulp) (string, error) {
281281
case UlpCompileTargetSubroutine:
282282
return fmt.Sprintf("move r2, %s\r\njump r2", c.dest.name(u)), nil
283283
default:
284-
return "", fmt.Errorf("Unknown compile target %d, please file a bug report", u.compileTarget)
284+
return "", fmt.Errorf("unknown compile target %d, please file a bug report", u.compileTarget)
285285
}
286286
}
287287

288288
func (c *CellBranch) OutputReference(u *Ulp) (string, error) {
289-
return "", fmt.Errorf("Cannot refer to a branch, please file a bug report")
289+
return "", fmt.Errorf("cannot refer to a branch, please file a bug report")
290290
}
291291

292292
func (c *CellBranch) IsRecursive(check *WordForth) bool {
@@ -330,12 +330,12 @@ func (c *CellBranch0) BuildExecution(u *Ulp) (string, error) {
330330
case UlpCompileTargetSubroutine:
331331
return fmt.Sprintf("move r1, %s\r\n%sjump __branch_if", c.dest.name(u), safeCall()), nil
332332
default:
333-
return "", fmt.Errorf("Unknown compile target %d, please file a bug report", u.compileTarget)
333+
return "", fmt.Errorf("unknown compile target %d, please file a bug report", u.compileTarget)
334334
}
335335
}
336336

337337
func (c *CellBranch0) OutputReference(u *Ulp) (string, error) {
338-
return "", fmt.Errorf("Cannot refer to a conditional branch, please file a bug report")
338+
return "", fmt.Errorf("cannot refer to a conditional branch, please file a bug report")
339339
}
340340

341341
func (c *CellBranch0) IsRecursive(check *WordForth) bool {
@@ -353,7 +353,7 @@ type CellTailCall struct {
353353
}
354354

355355
func (c *CellTailCall) Execute(vm *VirtualMachine) error {
356-
return fmt.Errorf("Cannot directly execute a tail call, please file a bug repot")
356+
return fmt.Errorf("cannot directly execute a tail call, please file a bug repot")
357357
}
358358

359359
func (c *CellTailCall) AddToList(u *Ulp) error {
@@ -369,12 +369,12 @@ func (c *CellTailCall) BuildExecution(u *Ulp) (string, error) {
369369
// put the address after the docol
370370
return fmt.Sprintf("move r2, %s\r\njump r2", c.dest.Entry.BodyLabel()), nil
371371
default:
372-
return "", fmt.Errorf("Unknown compile target %d, please file a bug report", u.compileTarget)
372+
return "", fmt.Errorf("unknown compile target %d, please file a bug report", u.compileTarget)
373373
}
374374
}
375375

376376
func (c *CellTailCall) OutputReference(u *Ulp) (string, error) {
377-
return "", fmt.Errorf("Cannot refer to a tail call, please file a bug report")
377+
return "", fmt.Errorf("cannot refer to a tail call, please file a bug report")
378378
}
379379

380380
func (c *CellTailCall) IsRecursive(check *WordForth) bool {

pkg/forth/dictionary.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (d *Dictionary) Setup(vm *VirtualMachine) error {
5858

5959
func (d *Dictionary) AddEntry(entry *DictionaryEntry) error {
6060
if d.Entries == nil {
61-
return fmt.Errorf("Dictionary not set up when adding entry, please file a bug report.")
61+
return fmt.Errorf("dictionary not set up when adding entry, please file a bug report")
6262
}
6363
name := entry.Name
6464
if name != "" {
@@ -88,7 +88,7 @@ func (d *Dictionary) standardizeName(name string) string {
8888

8989
func (d *Dictionary) FindName(name string) (*DictionaryEntry, error) {
9090
if d.Entries == nil {
91-
return nil, fmt.Errorf("Dictionary not set up when finding name, please file a bug report.")
91+
return nil, fmt.Errorf("dictionary not set up when finding name, please file a bug report")
9292
}
9393
nameLower := d.standardizeName(name)
9494
same, ok := d.entryMap[nameLower]
@@ -100,14 +100,14 @@ func (d *Dictionary) FindName(name string) (*DictionaryEntry, error) {
100100
}
101101
}
102102
}
103-
return nil, fmt.Errorf("%s not found in dictionary.", name)
103+
return nil, fmt.Errorf("%s not found in dictionary", name)
104104
}
105105

106106
func (d *Dictionary) LastForthWord() (*WordForth, error) {
107107
lastEntry := d.Entries[len(d.Entries)-1]
108108
last, ok := lastEntry.Word.(*WordForth)
109109
if !ok {
110-
return nil, fmt.Errorf("The last word in dictionary is not a Forth word.")
110+
return nil, fmt.Errorf("the last word in dictionary is not a Forth word")
111111
}
112112
return last, nil
113113
}

pkg/forth/optimize.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ func (o *Optimizer) removeDeferred() error {
101101
}
102102
data, ok := address.Entry.Word.(*WordForth)
103103
if !ok {
104-
return fmt.Errorf("error reading a deferred word, please file a bug report.")
104+
return fmt.Errorf("error reading a deferred word, please file a bug report")
105105
}
106106
if len(data.Cells) < 1 {
107-
return fmt.Errorf("deferred word not allocated, please file a bug report.")
107+
return fmt.Errorf("deferred word not allocated, please file a bug report")
108108
}
109109
embedded := data.Cells[0]
110110
exit := w.Cells[len(w.Cells)-1]

pkg/forth/stack.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (s *Stack) Push(c Cell) error {
3838
func (s *Stack) Pop() (Cell, error) {
3939
last := len(s.stack) - 1
4040
if last < 0 {
41-
return nil, fmt.Errorf("Attempted to pop empty stack.")
41+
return nil, fmt.Errorf("attempted to pop empty stack")
4242
}
4343
c := s.stack[last]
4444
s.stack = s.stack[:last]
@@ -53,7 +53,7 @@ func (s *Stack) PopNumber() (uint16, error) {
5353
}
5454
cellNumber, ok := cell.(CellNumber)
5555
if !ok {
56-
return 0, fmt.Errorf("Could not convert cell to number: %s type %T", cell, cell)
56+
return 0, fmt.Errorf("could not convert cell to number: %s type %T", cell, cell)
5757
}
5858
return cellNumber.Number, nil
5959
}
@@ -66,7 +66,7 @@ func (s *Stack) Depth() int {
6666
// Set the stack depth. Must not be greater than current depth.
6767
func (s *Stack) SetDepth(depth int) error {
6868
if depth > len(s.stack) {
69-
return fmt.Errorf("Cannot arbitrarily increase stack depth.")
69+
return fmt.Errorf("cannot arbitrarily increase stack depth")
7070
}
7171
s.stack = s.stack[:depth]
7272
return nil

pkg/forth/ulpBuild.go

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -29,56 +29,7 @@ const (
2929
UlpCompileTargetSubroutine
3030
)
3131

32-
type ulpAsm struct {
33-
name string
34-
asm []string
35-
}
36-
37-
func (uAsm ulpAsm) build() string {
38-
var sb strings.Builder
39-
sb.WriteString(uAsm.name)
40-
sb.WriteString(":\r\n")
41-
for _, asm := range uAsm.asm {
42-
if !strings.Contains(asm, ":") {
43-
sb.WriteString(" ")
44-
}
45-
sb.WriteString(asm)
46-
sb.WriteString("\r\n")
47-
}
48-
return sb.String()
49-
}
50-
51-
type ulpForthCell struct {
52-
cell Cell
53-
name string
54-
}
55-
56-
type ulpForth struct {
57-
name string
58-
cells []ulpForthCell
59-
}
60-
61-
func (uForth ulpForth) build() string {
62-
var sb strings.Builder
63-
sb.WriteString(uForth.name)
64-
sb.WriteString(":\r\n")
65-
for _, cell := range uForth.cells {
66-
if strings.Contains(cell.name, ":") {
67-
sb.WriteString(" ")
68-
} else {
69-
sb.WriteString(" .int ")
70-
}
71-
sb.WriteString(cell.name)
72-
sb.WriteString("\r\n")
73-
}
74-
return sb.String()
75-
}
76-
7732
type Ulp struct {
78-
// output strings
79-
assembly []ulpAsm
80-
forth []ulpForth
81-
data map[string]string
8233
outCount int
8334

8435
// output definitions
@@ -88,51 +39,19 @@ type Ulp struct {
8839
literals map[string]string
8940

9041
// current state of compilation
91-
compileType UlpCompileType
9242
compileTarget UlpCompileTarget
9343
}
9444

95-
func (u *Ulp) build() string {
96-
var sb strings.Builder
97-
// header
98-
sb.WriteString(u.buildInterpreter())
99-
// assembly
100-
sb.WriteString("\r\n.text\r\n")
101-
sb.WriteString("__asmwords_start:\r\n")
102-
for _, asm := range u.assembly {
103-
sb.WriteString(asm.build())
104-
}
105-
sb.WriteString("__asmwords_end:\r\n\r\n")
106-
// forth
107-
sb.WriteString(".data\r\n")
108-
sb.WriteString("__forthwords_start:\r\n")
109-
for _, forth := range u.forth {
110-
sb.WriteString(forth.build())
111-
}
112-
sb.WriteString("__forthwords_end:\r\n\r\n")
113-
// data
114-
sb.WriteString("__forthdata_start:\r\n")
115-
for _, d := range u.data {
116-
sb.WriteString(d)
117-
sb.WriteString("\r\n")
118-
}
119-
sb.WriteString("__forthdata_end:\r\n")
120-
return sb.String()
121-
}
122-
12345
// Build the assembly using the word passed in as the main function.
12446
// Note that the virtual machine will be unusable after this.
12547
func (u *Ulp) BuildAssembly(vm *VirtualMachine, word string) (string, error) {
12648
// put back into interpret state and compile the main ulp program
127-
u.assembly = make([]ulpAsm, 0)
128-
u.forth = make([]ulpForth, 0)
129-
u.data = make(map[string]string)
13049

13150
vm.State.Set(uint16(StateInterpret))
13251
// create the VM.INIT word without an EXIT
13352
err := vm.Execute([]byte(" BL WORD VM.INIT --CREATE-FORTH ] VM.STACK.INIT " + word + " BEGIN HALT AGAIN [ LAST HIDE "))
13453
if err != nil {
135-
return "", errors.Join(fmt.Errorf("could not compile the supporting words for ulp cross-compiling."), err)
54+
return "", errors.Join(fmt.Errorf("could not compile the supporting words for ulp cross-compiling"), err)
13655
}
13756
u.compileTarget = UlpCompileTargetToken
13857
return u.buildAssemblyHelper(vm, u.buildInterpreter())
@@ -143,7 +62,7 @@ func (u *Ulp) BuildAssemblySrt(vm *VirtualMachine, word string) (string, error)
14362
// create the VM.INIT word without an EXIT
14463
err := vm.Execute([]byte(" BL WORD VM.INIT --CREATE-FORTH ] " + word + " BEGIN HALT AGAIN [ LAST HIDE "))
14564
if err != nil {
146-
return "", errors.Join(fmt.Errorf("could not compile the supporting words for ulp cross-compiling."), err)
65+
return "", errors.Join(fmt.Errorf("could not compile the supporting words for ulp cross-compiling"), err)
14766
}
14867
u.compileTarget = UlpCompileTargetSubroutine
14968
return u.buildAssemblyHelper(vm, u.buildInterpreterSrt())
@@ -273,7 +192,7 @@ func (u *Ulp) buildLiterals() (string, error) {
273192
case UlpCompileTargetSubroutine:
274193
return "", nil // literals are compiled along the way, not as a final list
275194
default:
276-
return "", fmt.Errorf("Unknown compile target %d, please file a bug report", u.compileTarget)
195+
return "", fmt.Errorf("unknown compile target %d, please file a bug report", u.compileTarget)
277196
}
278197
}
279198

pkg/forth/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import "fmt"
1212
func GetCellNumber(c Cell) (CellNumber, error) {
1313
cellNumber, ok := c.(CellNumber)
1414
if !ok {
15-
return CellNumber{}, fmt.Errorf("Attempted to convert cell to number: %v", c)
15+
return CellNumber{}, fmt.Errorf("attempted to convert cell to number: %v", c)
1616
}
1717
return cellNumber, nil
1818
}

pkg/forth/vm.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ var builtinsEsp32 embed.FS
106106
func (vm *VirtualMachine) buildEmbed(f embed.FS, name string) error {
107107
dirEntries, err := f.ReadDir(name)
108108
if err != nil {
109-
return errors.Join(fmt.Errorf("Error while opening embedded directory."), err)
109+
return errors.Join(fmt.Errorf("error while opening embedded directory"), err)
110110
}
111111
for _, entry := range dirEntries {
112112
// don't look in subdirectories (for now)
@@ -162,7 +162,7 @@ func (w writerNoNewline) Write(p []byte) (int, error) {
162162
func (vm *VirtualMachine) ReplSetup() error {
163163
rl, err := readline.New("")
164164
if err != nil {
165-
return errors.Join(fmt.Errorf("Unable to start readline, please file a bug report."), err)
165+
return errors.Join(fmt.Errorf("unable to start readline, please file a bug report"), err)
166166
}
167167
vm.repl = rl
168168
return nil
@@ -264,7 +264,7 @@ func (vm *VirtualMachine) executeLine(bytes []byte) error {
264264
case StateExit:
265265
return nil // the repl will exit after reading the state
266266
default:
267-
return fmt.Errorf("Unknown state %d", state)
267+
return fmt.Errorf("unknown state %d", state)
268268
}
269269
}
270270
}

0 commit comments

Comments
 (0)