Skip to content

Commit

Permalink
We know have an std lib implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Nov 13, 2024
1 parent 8311d2a commit 65f84ce
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 22 deletions.
23 changes: 13 additions & 10 deletions lib/std.msh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

# each (list quote --)
def each
over len # Get total length
0 # index
over len @each-len # Get total length
0 @each-idx # index
(
[(over over <=) (break)] if
# list quote len index index
dup 5 pick nth
# list quote len index item
4 pick x
drop
1 + # inc index
[(each-idx! each-len! >=) (break)] if
over each-idx! nth # Get current item
over x # Copy over quote, execute
each-idx! 1 + @each-idx # inc index
) loop

# Drop list and quote, total length, index
drop drop drop drop
drop drop
end

# map (list quote -- list)
Expand All @@ -37,6 +34,12 @@ def map
drop drop map-accum!
end

# .. (-- str)
def .. stdin end

# tt = Tab separated Table (-- list[list[str]])
def tt stdin lines (" " split) map end

# map (list quote -- list)
# [seq 1 1000]o; ("Line #" over str + wl) each
# [1 2 3] (2 +) map .s
2 changes: 1 addition & 1 deletion mshell-go/Evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func (state *EvalState) Evaluate(objects []MShellParseItem, stack *MShellStack,
}
}
default:
return FailWithMessage(fmt.Sprintf("%d:%d: Cannot join a %s.\n", t.Line, t.Column, list.TypeName()))
return FailWithMessage(fmt.Sprintf("%d:%d: Cannot join a %s (%s).\n", t.Line, t.Column, list.TypeName(), list.DebugString()))
}

stack.Push(&MShellString { strings.Join(listItems, delimiterStr) })
Expand Down
53 changes: 42 additions & 11 deletions mshell-go/Main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,6 @@ func main() {
return
}

p := MShellParser{ lexer: l }
p.NextToken()
file, err := p.ParseFile()
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing file %s: %s\n", input, err)
os.Exit(1)
return
}

// tokens := l.Tokenize()
state := EvalState {
PositionalArgs: positionalArgs,
LoopDepth: 0,
Expand All @@ -128,7 +118,48 @@ func main() {
StandardOutput: os.Stdout,
}

result := state.Evaluate(file.Items, &stack, context, file.Definitions)
var allDefinitions []MShellDefinition

// Check for environment variable MSHSTDLIB and load that file. Read as UTF-8
stdlibPath, stdlibSet := os.LookupEnv("MSHSTDLIB")
if stdlibSet {
stdlibBytes, err := os.ReadFile(stdlibPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading file %s: %s\n", stdlibPath, err)
os.Exit(1)
return
}
stdlibLexer := NewLexer(string(stdlibBytes))
stdlibParser := MShellParser{ lexer: stdlibLexer }
stdlibParser.NextToken()
stdlibFile, err := stdlibParser.ParseFile()
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing file %s: %s\n", stdlibPath, err)
os.Exit(1)
return
}

allDefinitions = append(allDefinitions, stdlibFile.Definitions...)
result := state.Evaluate(stdlibFile.Items, &stack, context, allDefinitions)

if !result.Success {
fmt.Fprintf(os.Stderr, "Error evaluating MSHSTDLIB file %s.\n", stdlibPath)
os.Exit(1)
return
}
}

p := MShellParser{ lexer: l }
p.NextToken()
file, err := p.ParseFile()
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing file %s: %s\n", input, err)
os.Exit(1)
return
}

allDefinitions = append(allDefinitions, file.Definitions...)
result := state.Evaluate(file.Items, &stack, context, allDefinitions)

if !result.Success {
os.Exit(1)
Expand Down
3 changes: 3 additions & 0 deletions mshell/tests/std_lib.msh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[1 2 3] (2 + str) map ", " join wl

[1 2 3] ([printf "%d\n"] append ;) each
4 changes: 4 additions & 0 deletions mshell/tests/std_lib.msh.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3, 4, 5
1
2
3
3 changes: 3 additions & 0 deletions mshell/tests/test_file_go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
TMP_FILE="$(mktemp)"
TMP_ERR="$(mktemp)"

MSHSTDLIB="$(realpath ../../lib/std.msh)"
export MSHSTDLIB

if printf %s "$1" | grep -q 'positional'; then
mshell-go "$1" Hello World > "$TMP_FILE" 2>"$TMP_ERR"
elif test "$(basename "$1")" = "args.msh"; then
Expand Down

0 comments on commit 65f84ce

Please sign in to comment.