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)
|
||||
|
||||
Reference in New Issue
Block a user