Skip to content

Commit

Permalink
saving the xml test compiler in a separate dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaiah Becker-Mayer committed Mar 28, 2021
1 parent cb68ca2 commit c834139
Show file tree
Hide file tree
Showing 31 changed files with 8,141 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"stdio.h": "c",
"symboltable.h": "c"
},
"python.pythonPath": "./venv/bin/python"
"python.pythonPath": "/usr/local/bin/python3"
}
11 changes: 11 additions & 0 deletions TestCompiler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TestCompiler

A first draft for the compiler that spits out XML for testing purposes. The book suggests this be modified in order to create the final compiler that spits out actual VM code. In order to keep this xml test version, though, I decided to save a separate copy of this XML version. The final compiler is in the Compiler directory.

#### Testing

To run all the tests from the command line, run

```
go test ./test/
```
4 changes: 4 additions & 0 deletions TestCompiler/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module github.com/ibeckermayer/Nand2TetrisFPGA/Compiler

go 1.15

1 change: 1 addition & 0 deletions TestCompiler/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/ibeckermayer/Nand2TetrisFPGA v0.0.0-20210122031511-4a0dddc9b098 h1:j3186jVNdOZV30DubToqydS37xNZEmzKFHrtPbMpFnM=
63 changes: 63 additions & 0 deletions TestCompiler/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/ibeckermayer/Nand2TetrisFPGA/Compiler/pkg/compiler"
)

// isJack determines whether a string ends in ".jack" or not
func isJack(in string) bool {
split := strings.Split(in, ".")
return split[len(split)-1] == "jack"
}

// jackFilter filters a list of strings down to only those which end in ".jack"
func jackFilter(ss []string) (ret []string) {
for _, s := range ss {
if isJack(s) {
ret = append(ret, s)
}
}
return
}

func main() {
// First argument should be .jack file or directory of .jack files. Currently
// do not support multiple directory builds.
if len(os.Args) < 2 {
panic("program requires the first argument be a path to .jack file or directory that contains at least one .jack file")
}
toCompile := os.Args[1]
var files []string

// try to list all the files in a directory by the name of the first command line argument,
// or just list the single jack file argument
if err := filepath.Walk(toCompile, func(path string, info os.FileInfo, err error) error {
if path != toCompile && info.IsDir() {
// only walk a single level of directories
return filepath.SkipDir
}
if isJack(path) {
files = append(files, path)
}
return nil
}); err != nil {
panic(err.Error())
}
if len(files) == 0 {
panic(fmt.Sprintf("invalid compilation input: \"%v\"; first argument must be either a .jack file or a directory with at least one .jack file in it", toCompile))
}

// for each jack file
for _, filePath := range files {
ce, err := compiler.New(filePath)
err = ce.Run()
if err != nil {
panic(err)
}
}
}
Loading

0 comments on commit c834139

Please sign in to comment.