Skip to content

Commit

Permalink
adding scriptlocation to interpreter, import function and dragup expe…
Browse files Browse the repository at this point in the history
…riment
  • Loading branch information
refaktor committed Apr 2, 2024
1 parent 9e86292 commit a76cc8b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ data.txt
bin/
dist/
tests/*.html
shell_*.rye
console_*.rye
3 changes: 3 additions & 0 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ type ProgramState struct {
ForcedResult Object
SkipFlag bool
InErrHandler bool
ScriptPath string // holds the path to the script that is being imported (doed) currently
}

func NewProgramState(ser TSeries, idx *Idxs) *ProgramState {
Expand All @@ -282,6 +283,7 @@ func NewProgramState(ser TSeries, idx *Idxs) *ProgramState {
nil,
false,
false,
"",
}
return &ps
}
Expand All @@ -303,6 +305,7 @@ func NewProgramStateNEW() *ProgramState {
nil,
false,
false,
"",
}
return &ps
}
Expand Down
25 changes: 25 additions & 0 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,31 @@ var builtins = map[string]*env.Builtin{
},
},

"import": { // **
Argsn: 1,
Doc: "Imports a file, loads and does it from script local path.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s1 := arg0.(type) {
case env.Uri:
var str string
fileIdx, _ := ps.Idx.GetIndex("file")
if s1.Scheme.Index == fileIdx {
b, err := os.ReadFile(s1.GetPath())
if err != nil {
return makeError(ps, err.Error())
}
str = string(b) // convert content to a 'string'
}
block, _ := loader.LoadString(str, false)
//ps = env.AddToProgramState(ps, block.Series, genv)
return block
default:
ps.FailureFlag = true
return env.NewError("Must be string or file TODO")
}
},
},

"load": { // **
Argsn: 1,
Doc: "Loads a string into Rye values.",
Expand Down
2 changes: 1 addition & 1 deletion examples/99beers.rye
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private {
fn\cc { n } {
prn join [ verse1 n ", " verse2 n "." newline verse3 ]
n - 1 :m \either
{ verse1 m |+ "." \print , beers m }
{ verse1 m \+ "." \print , beers m }
{ print "no more bottles of beer on the wall." }
}
} :beers
Expand Down
74 changes: 74 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,80 @@ func main_rye_file(file string, sig bool, subc bool, interactive bool, code stri
}
}

ps := env.NewProgramStateNEW()
evaldo.RegisterBuiltins(ps)
contrib.RegisterBuiltins(ps, &evaldo.BuiltinNames)
// ctx := ps.Ctx
// ps.Ctx = env.NewEnv(ctx)
//ES = ps
// evaldo.ShowResults = false

block := loader.LoadStringNEW(" "+content+" ", sig, ps)
switch val := block.(type) {
case env.Block:

// block, genv := loader.LoadString(content+"\n"+code, sig)
// switch val := block.(type) {
// case env.Block:
//es := env.NewProgramState(block.(env.Block).Series, genv)
//evaldo.RegisterBuiltins(es)
// contrib.RegisterBuiltins(es, &evaldo.BuiltinNames)

ps = env.AddToProgramState(ps, val.Series, ps.Idx)

if subc {
ctx := ps.Ctx
ps.Ctx = env.NewEnv(ctx)
}

evaldo.EvalBlock(ps)
evaldo.MaybeDisplayFailureOrError(ps, ps.Idx)

if interactive {
evaldo.DoRyeRepl(ps, evaldo.ShowResults)
}

case env.Error:
fmt.Println(val.Message)
}
}

func main_rye_file_OLD(file string, sig bool, subc bool, interactive bool, code string) {
info := true
//util.PrintHeader()
//defer profile.Start(profile.CPUProfile).Stop()

var content string

if file[len(file)-4:] == ".enc" {
fmt.Print("Enter Password: ")
bytePassword, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
password := string(bytePassword)

content = util.ReadSecure(file, password)
} else {
bcontent, err := os.ReadFile(file)
if err != nil {
log.Fatal(err)
}
content = string(bcontent)
}

if info {
pattern := regexp.MustCompile(`^; (#[^\n]*)`)

lines := pattern.FindAllStringSubmatch(content, -1)

for _, line := range lines {
if line[1] != "" {
fmt.Println(line[1])
}
}
}

block, genv := loader.LoadString(content+"\n"+code, sig)
switch val := block.(type) {
case env.Block:
Expand Down
2 changes: 2 additions & 0 deletions wasm/dragup/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ document.addEventListener('DOMContentLoaded', (event) => {
let reader = new FileReader();
reader.onload = function(e) {
fileContent.style.display = 'block';
fileContent.style.left = "670px";
fileContent.style.top = "40px";
fileContent.innerText = e.target.result;
};
reader.readAsText(file);
Expand Down

0 comments on commit a76cc8b

Please sign in to comment.