Skip to content

Commit

Permalink
Swap store/restore syntax to align for Forth, Porth
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Nov 14, 2024
1 parent 43e3d13 commit 5e02c58
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 27 deletions.
26 changes: 13 additions & 13 deletions lib/std.msh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

# each (list quote --)
def each
over len @each-len # Get total length
0 @each-idx # index
over len each-len! # Get total length
0 each-idx! # index
(
[(each-idx! each-len! >=) (break)] if
over each-idx! nth # Get current item
[(@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
@each-idx 1 + each-idx! # inc index
) loop

# Drop list and quote, total length, index
Expand All @@ -17,21 +17,21 @@ end

# map (list quote -- list)
def map
over len @map-len # Get total length
0 @map-idx # Index
[] @map-accum # Accumulator
over len map-len! # Get total length
0 map-idx! # Index
[] map-accum! # Accumulator
(
[(map-idx! map-len! >=) (break)] if
over map-idx! nth # Get current item
[(@map-idx @map-len >=) (break)] if
over @map-idx nth # Get current item
over x # Copy over quote, execute

# list quote new-item
map-accum! append drop # Append to accumulator
map-idx! 1 + @map-idx # inc index
@map-accum append drop # Append to accumulator
@map-idx 1 + map-idx! # inc index
) loop

# Drop list and quote, push accumulator
drop drop map-accum!
drop drop @map-accum
end


Expand Down
7 changes: 4 additions & 3 deletions mshell-go/Evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,13 +876,14 @@ MainLoop:
}
} else if t.Type == VARSTORE {
obj, err := stack.Pop()
varName := t.Lexeme[0:len(t.Lexeme)-1] // Remove the trailing !
if err != nil {
return FailWithMessage(fmt.Sprintf("%d:%d: Nothing on stack to store into variable %s.\n", t.Line, t.Column, t.Lexeme[1:]))
return FailWithMessage(fmt.Sprintf("%d:%d: Nothing on stack to store into variable %s.\n", t.Line, t.Column, varName))
}

state.Variables[t.Lexeme[1:]] = obj
state.Variables[varName] = obj
} else if t.Type == VARRETRIEVE {
name := t.Lexeme[:len(t.Lexeme)-1]
name := t.Lexeme[1:] // Remove the leading @
obj, found_mshell_variable := state.Variables[name]
if found_mshell_variable {
stack.Push(obj)
Expand Down
4 changes: 2 additions & 2 deletions mshell-go/Lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,10 @@ func (l *Lexer) parseLiteralOrNumber() Token {
return l.makeToken(FALSE)
default:
if strings.HasSuffix(literal, "!") {
return l.makeToken(VARRETRIEVE)
return l.makeToken(VARSTORE)
}
if strings.HasPrefix(literal, "@") {
return l.makeToken(VARSTORE)
return l.makeToken(VARRETRIEVE)
}
if _, err := strconv.Atoi(literal); err == nil {
return l.makeToken(INTEGER)
Expand Down
2 changes: 1 addition & 1 deletion mshell/tests/env_test.msh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"Hello, World!" @MSHELL_VAR MSHELL_VAR export
"Hello, World!" MSHELL_VAR! MSHELL_VAR export
[./env_test.sh];
8 changes: 4 additions & 4 deletions mshell/tests/or_equal_to_comparers.msh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
0
(
[(dup 10 >=) (break)] if
dup @a
[echo a!];
dup a!
[echo @a];
1 +
) loop

[echo "Now go backwards"];
(
[(dup 0 <=) (break)] if
dup @a
[echo a!];
dup a!
[echo @a];
1 -
) loop
2 changes: 1 addition & 1 deletion mshell/tests/tilde.msh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"/home/myuser" @HOME HOME export
"/home/myuser" HOME! HOME export
[echo ~/repos/];
4 changes: 2 additions & 2 deletions mshell/tests/variables.msh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
10 @a
[echo a!];
10 a!
[echo @a];
2 changes: 1 addition & 1 deletion mshell/tests/while_read.msh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1
(
read [(not) (break)] if
@linetext @num "Line " num! str ": " + + linetext! + [echo] append ; num! 1 +
linetext! num! "Line " @num str ": " + + @linetext + [echo] append ; @num 1 +
) "stdin_for_test.txt" < loop

0 comments on commit 5e02c58

Please sign in to comment.