wip: interpreter
This commit is contained in:
Binary file not shown.
@@ -13,6 +13,32 @@ import (
|
||||
)
|
||||
|
||||
|
||||
func Any[T any](ts []T, pred func(T) bool) bool {
|
||||
for _, t := range ts {
|
||||
if pred(t) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func parseLabelledOperand(line string, preprocessed *PreprocessorResult) (int, error) {
|
||||
op := 0;
|
||||
dest := strings.Fields(line)[1]
|
||||
containsLetter := Any([]rune(dest), unicode.IsLetter)
|
||||
if (containsLetter) {
|
||||
op = preprocessed.LabelMap[dest]
|
||||
} else {
|
||||
op, err := strconv.Atoi(dest)
|
||||
if err != nil {
|
||||
return op, err
|
||||
}
|
||||
}
|
||||
|
||||
return op, nil
|
||||
}
|
||||
|
||||
|
||||
var InstructionTable map[string]Operation = map[string]Operation{
|
||||
"HALT": 0,
|
||||
"PUSH": 1,
|
||||
@@ -64,20 +90,13 @@ func PreprocessAssembly(f *os.File) (*PreprocessorResult, error) {
|
||||
return &PreprocessorResult{ LabelMap: labelMap, Listing: &buf }, nil
|
||||
}
|
||||
|
||||
func parseProgramFromFile(m* Machine, filename string) error {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return errors.New("Couldn't open file")
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
func parseProgramFromFile(m* Machine, f *os.File) error {
|
||||
preprocessed, err := PreprocessAssembly(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(preprocessed.LabelMap)
|
||||
scanner := bufio.NewScanner(preprocessed.Listing)
|
||||
|
||||
scanner := bufio.NewScanner(preprocessed.Listing)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if (strings.HasPrefix(line, "//")) {
|
||||
@@ -152,35 +171,17 @@ func parseProgramFromFile(m* Machine, filename string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Any[T any](ts []T, pred func(T) bool) bool {
|
||||
for _, t := range ts {
|
||||
if pred(t) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func parseLabelledOperand(line string, preprocessed *PreprocessorResult) (int, error) {
|
||||
op := 0;
|
||||
dest := strings.Fields(line)[1]
|
||||
containsLetter := Any([]rune(dest), unicode.IsLetter)
|
||||
if (containsLetter) {
|
||||
op = preprocessed.LabelMap[dest]
|
||||
} else {
|
||||
op, err := strconv.Atoi(dest)
|
||||
if err != nil {
|
||||
return op, err
|
||||
}
|
||||
}
|
||||
|
||||
return op, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
f, err := os.Open("program.lil")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[ERR] Couldn't open file: %s\n", err);
|
||||
os.Exit(1)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
m := Constructor()
|
||||
|
||||
err := parseProgramFromFile(m, "../program.lil")
|
||||
err = parseProgramFromFile(m, f)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[ERR] %s\n", err);
|
||||
os.Exit(1)
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
package elm
|
||||
import (
|
||||
// "unicode"
|
||||
// "bytes"
|
||||
// "bufio"
|
||||
// "strings"
|
||||
"fmt"
|
||||
//"os"
|
||||
"errors"
|
||||
// "encoding/binary"
|
||||
// "strconv"
|
||||
)
|
||||
|
||||
const STACK_SIZE = 1024
|
||||
|
||||
7
interpreter/go.mod
Normal file
7
interpreter/go.mod
Normal file
@@ -0,0 +1,7 @@
|
||||
module e1lama/interpreter
|
||||
|
||||
go 1.20
|
||||
|
||||
replace e1lama/elm => ../core
|
||||
|
||||
require e1lama/elm v0.0.0-00010101000000-000000000000
|
||||
4
interpreter/go.sum
Normal file
4
interpreter/go.sum
Normal file
@@ -0,0 +1,4 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
37
interpreter/interpreter.go
Normal file
37
interpreter/interpreter.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"unicode"
|
||||
"bytes"
|
||||
"bufio"
|
||||
"strings"
|
||||
"strconv"
|
||||
"os"
|
||||
"fmt"
|
||||
"errors"
|
||||
. "e1lama/elm"
|
||||
)
|
||||
|
||||
|
||||
func LoadProgramFromBinary(m *Machine, f *os.File) error {
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
f, err := os.Open("program.elb")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[ERR] Couldn't open file: %s\n", err);
|
||||
os.Exit(1)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
m := Constructor()
|
||||
|
||||
err = LoadProgramFromBinary(m, f)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[ERR] Error reading binary: %s\n", err);
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
m.Run()
|
||||
}
|
||||
Reference in New Issue
Block a user