2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-07-01 16:52:03 +00:00

feat: impl more functions, uncomment nature code

This commit is contained in:
sammyette 2025-06-14 10:31:17 -04:00
parent 9aa0a499c9
commit 9c0819969f
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
9 changed files with 32 additions and 34 deletions

5
api.go
View File

@ -299,10 +299,11 @@ func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// cwd() -> string // cwd() -> string
// Returns the current directory of the shell. // Returns the current directory of the shell.
// #returns string // #returns string
func hlcwd(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { func hlcwd(mlr *moonlight.Runtime) error {
cwd, _ := os.Getwd() cwd, _ := os.Getwd()
return mlr.PushNext1(c, moonlight.StringValue(cwd)), nil mlr.PushNext1(moonlight.StringValue(cwd))
return nil
} }
// read(prompt) -> input (string) // read(prompt) -> input (string)

View File

@ -117,7 +117,7 @@ func (c *Commander) cderegister(mlr *moonlight.Runtime) error {
// Returns all registered commanders. Returns a list of tables with the following keys: // Returns all registered commanders. Returns a list of tables with the following keys:
// - `exec`: The function used to run the commander. Commanders require args and sinks to be passed. // - `exec`: The function used to run the commander. Commanders require args and sinks to be passed.
// #returns table // #returns table
func (c *Commander) cregistry(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moonlight.Cont, error) { /*func (c *Commander) cregistry(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moonlight.Cont, error) {
registryLua := moonlight.NewTable() registryLua := moonlight.NewTable()
for cmdName, cmd := range c.Commands { for cmdName, cmd := range c.Commands {
cmdTbl := moonlight.NewTable() cmdTbl := moonlight.NewTable()
@ -129,4 +129,4 @@ func (c *Commander) cregistry(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moo
} }
return mlr.PushNext1(ct, moonlight.TableValue(registryLua)), nil return mlr.PushNext1(ct, moonlight.TableValue(registryLua)), nil
} }*/

View File

@ -39,14 +39,14 @@ func (f *fs) Loader(rtm *moonlight.Runtime) moonlight.Value {
"cd": util.LuaExport{f.fcd, 1, false}, "cd": util.LuaExport{f.fcd, 1, false},
"mkdir": util.LuaExport{f.fmkdir, 2, false}, "mkdir": util.LuaExport{f.fmkdir, 2, false},
"stat": util.LuaExport{f.fstat, 1, false}, "stat": util.LuaExport{f.fstat, 1, false},
"readdir": {f.freaddir, 1, false},
"abs": util.LuaExport{f.fabs, 1, false}, "abs": util.LuaExport{f.fabs, 1, false},
"basename": util.LuaExport{f.fbasename, 1, false}, "basename": util.LuaExport{f.fbasename, 1, false},
"dir": {f.fdir, 1, false},
"glob": util.LuaExport{f.fglob, 1, false}, "glob": util.LuaExport{f.fglob, 1, false},
"join": util.LuaExport{f.fjoin, 0, true}, "join": util.LuaExport{f.fjoin, 0, true},
"pipe": util.LuaExport{f.fpipe, 0, false}, "pipe": util.LuaExport{f.fpipe, 0, false},
*/ */
"readdir": {f.freaddir, 1, false},
"dir": {f.fdir, 1, false},
} }
mod := moonlight.NewTable() mod := moonlight.NewTable()
@ -122,8 +122,7 @@ func (f *fs) fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// `~/Documents/doc.txt` then this function will return `~/Documents`. // `~/Documents/doc.txt` then this function will return `~/Documents`.
// #param path string Path to get the directory for. // #param path string Path to get the directory for.
// #returns string // #returns string
/* func (f *fs) fdir(mlr *moonlight.Runtime) error {
func (f *fs) fdir(mlr *moonlight.Runtime, c *moonlight.GoCont) error {
if err := mlr.Check1Arg(); err != nil { if err := mlr.Check1Arg(); err != nil {
return err return err
} }
@ -132,11 +131,9 @@ func (f *fs) fdir(mlr *moonlight.Runtime, c *moonlight.GoCont) error {
return err return err
} }
println(patg) mlr.PushNext1(moonlight.StringValue(filepath.Dir(path)))
//next := mlr.PushNext1(c, moonlight.StringValue(filepath.Dir(path)))
return nil return nil
} }
*/
// glob(pattern) -> matches (table) // glob(pattern) -> matches (table)
// Match all files based on the provided `pattern`. // Match all files based on the provided `pattern`.
@ -265,26 +262,27 @@ func (f *fs) fpipe(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// Returns a list of all files and directories in the provided path. // Returns a list of all files and directories in the provided path.
// #param dir string // #param dir string
// #returns table // #returns table
func (f *fs) freaddir(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { func (f *fs) freaddir(mlr *moonlight.Runtime) error {
if err := mlr.Check1Arg(); err != nil { if err := mlr.Check1Arg(); err != nil {
return nil, err return err
} }
dir, err := mlr.StringArg(0) dir, err := mlr.StringArg(0)
if err != nil { if err != nil {
return nil, err return err
} }
dir = util.ExpandHome(dir) dir = util.ExpandHome(dir)
names := moonlight.NewTable() names := moonlight.NewTable()
dirEntries, err := os.ReadDir(dir) dirEntries, err := os.ReadDir(dir)
if err != nil { if err != nil {
return nil, err return err
} }
for i, entry := range dirEntries { for i, entry := range dirEntries {
names.Set(moonlight.IntValue(int64(i+1)), moonlight.StringValue(entry.Name())) names.Set(moonlight.IntValue(int64(i+1)), moonlight.StringValue(entry.Name()))
} }
return mlr.PushNext1(c, moonlight.TableValue(names)), nil mlr.PushNext1(moonlight.TableValue(names))
return nil
} }
// stat(path) -> {} // stat(path) -> {}

View File

@ -3,8 +3,6 @@
package terminal package terminal
import ( import (
"os"
"hilbish/moonlight" "hilbish/moonlight"
"golang.org/x/term" "golang.org/x/term"
@ -28,6 +26,7 @@ func Loader(rtm *moonlight.Runtime) moonlight.Value {
return moonlight.TableValue(mod) return moonlight.TableValue(mod)
} }
/*
// size() // size()
// Gets the dimensions of the terminal. Returns a table with `width` and `height` // Gets the dimensions of the terminal. Returns a table with `width` and `height`
// NOTE: The size refers to the amount of columns and rows of text that can fit in the terminal. // NOTE: The size refers to the amount of columns and rows of text that can fit in the terminal.
@ -77,3 +76,4 @@ func termsetRaw(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, er
return c.Next(), nil return c.Next(), nil
} }
*/

View File

@ -41,6 +41,8 @@ func (mlr *Runtime) Arg(c *GoCont, num int) Value {
} }
func (mlr *Runtime) GoFunction(fun GoToLuaFunc) *GoFunctionFunc { func (mlr *Runtime) GoFunction(fun GoToLuaFunc) *GoFunctionFunc {
mlr.returnNum = 0
return &GoFunctionFunc{ return &GoFunctionFunc{
cf: func(L *lua.State) int { cf: func(L *lua.State) int {
err := fun(mlr) err := fun(mlr)
@ -57,7 +59,7 @@ func (mlr *Runtime) GoFunction(fun GoToLuaFunc) *GoFunctionFunc {
}*/ }*/
//return len(cont.(*GoCont).vals) //return len(cont.(*GoCont).vals)
return 0 return mlr.returnNum
}, },
} }
} }

View File

@ -8,6 +8,7 @@ import (
type Runtime struct { type Runtime struct {
state *lua.State state *lua.State
returnNum int
} }
func NewRuntime() *Runtime { func NewRuntime() *Runtime {
@ -19,10 +20,10 @@ func NewRuntime() *Runtime {
} }
} }
func (mlr *Runtime) PushNext1(c *GoCont, v Value) Cont { func (mlr *Runtime) PushNext1(v Value) {
c.vals = []Value{v} mlr.returnNum = 1
return c mlr.pushToState(v)
} }
func (mlr *Runtime) Call1(f Value, args ...Value) (Value, error) { func (mlr *Runtime) Call1(f Value, args ...Value) (Value, error) {

View File

@ -1,15 +1,16 @@
//go:build !midnight //go:build !midnight
package moonlight package moonlight
import ( import (
"os" "os"
rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib" "github.com/arnodel/golua/lib"
"github.com/arnodel/golua/lib/debuglib" "github.com/arnodel/golua/lib/debuglib"
rt "github.com/arnodel/golua/runtime"
) )
type Runtime struct{ type Runtime struct {
rt *rt.Runtime rt *rt.Runtime
} }
@ -40,8 +41,8 @@ func (mlr *Runtime) Push(c *GoCont, v Value) {
c.cont.Push(c.thread.Runtime, v) c.cont.Push(c.thread.Runtime, v)
} }
func (mlr *Runtime) PushNext1(c *GoCont, v Value) Cont { func (mlr *Runtime) PushNext1(v Value) {
return c.cont.PushingNext1(c.thread.Runtime, v) mlr.rt.MainThread().CurrentCont().(*rt.GoCont).Next().Push(mlr.rt.MainThread().Runtime, v)
} }
func (mlr *Runtime) Call1(val Value, args ...Value) (Value, error) { func (mlr *Runtime) Call1(val Value, args ...Value) (Value, error) {

View File

@ -6,7 +6,8 @@ local fs = require 'fs'
-- we will use that to automatically load all commands by reading -- we will use that to automatically load all commands by reading
-- all the files in this dir and just requiring it. -- all the files in this dir and just requiring it.
local info = debug.getinfo(1) local info = debug.getinfo(1)
local commandDir = fs.dir(info.source) local commandDir = fs.dir(info.source:match './.+')
print(commandDir)
if commandDir == '.' then return end if commandDir == '.' then return end
local commands = fs.readdir(commandDir) local commands = fs.readdir(commandDir)

View File

@ -1,9 +1,4 @@
-- Prelude initializes everything else for our shell -- Prelude initializes everything else for our shell
print(require 'hilbish')
print('hilbitch type', type(hilbish))
print(hilbish.prompt)
print(hilbish.ver)
--[[
local _ = require 'succulent' -- Function additions local _ = require 'succulent' -- Function additions
local bait = require 'bait' local bait = require 'bait'
--local fs = require 'fs' --local fs = require 'fs'
@ -97,4 +92,3 @@ end)
bait.catch('command.not-executable', function(cmd) bait.catch('command.not-executable', function(cmd)
print(string.format('hilbish: %s: not executable', cmd)) print(string.format('hilbish: %s: not executable', cmd))
end) end)
]]--