Start supporting numbers and strings

This commit is contained in:
2023-02-25 16:45:54 +00:00
parent cbae1c4629
commit 636101f1fa
15 changed files with 379 additions and 37 deletions

102
src/import.go Normal file
View File

@@ -0,0 +1,102 @@
package main
import (
"bufio"
"errors"
"log"
"os"
"path/filepath"
)
func FileExists(filename string) bool {
if _, err := os.Stat(filename); err == nil {
return true
} else if errors.Is(err, os.ErrNotExist) {
return false
} else {
return false
}
}
func readFile(path string) []UNPARSEcode {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
return nil
}
defer file.Close()
scanner := bufio.NewScanner(file)
// optionally, resize scanner's capacity for lines over 64K, see next example
output := []UNPARSEcode{}
line := 1
for scanner.Scan() {
output = append(output, UNPARSEcode{scanner.Text(), line})
line++
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
return nil
}
return output
}
func importMod(realpath string, origin string, main bool) string {
extention := filepath.Ext(realpath)
path := realpath
if extention == "" {
path += ".ar"
}
ex, err := os.Getwd()
if err != nil {
return err.Error()
}
executable, err := os.Executable()
if err != nil {
return err.Error()
}
executable = filepath.Dir(executable)
isABS := filepath.IsAbs(path)
var pathsToTest []string
if isABS {
pathsToTest = []string{
filepath.Join(path),
filepath.Join(realpath, "init.ar"),
}
} else {
pathsToTest = []string{
filepath.Join(origin, realpath, "init.ar"),
filepath.Join(origin, path),
filepath.Join(origin, "modules", path),
filepath.Join(origin, "modules", realpath, "init.ar"),
filepath.Join(ex, path),
filepath.Join(ex, "modules", realpath, "init.ar"),
filepath.Join(ex, "modules", path),
filepath.Join(executable, "modules", realpath, "init.ar"),
filepath.Join(executable, "modules", path),
}
}
var p string
var found bool
for _, p = range pathsToTest {
if FileExists(p) {
found = true
break
}
}
if !found {
return "File does not exist: " + realpath
}
codelines := readFile(p)
translated, translationerr := translate(codelines)
if translationerr != "" {
return translationerr
}
run(translated)
return ""
}