Skip to content

Commit

Permalink
Adding syntax for definition typing (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus authored Dec 15, 2024
1 parent 543bba7 commit 634a66d
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 39 deletions.
4 changes: 2 additions & 2 deletions DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ typeItem
typeQuote : '(' type* -- type* ')' ;
typeList : homogeneousList | heterogeneousList ;
homogeneousList : '[' type '*' ']' ;
heterogeneousList : '[' type+ ']' ;
homogeneousList : '[' type ']' ;
heterogeneousList : '&' '[' type* ']' ;
```

Key Types:
Expand Down
46 changes: 19 additions & 27 deletions lib/std.msh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

# type numeric int | float

# each (list quote: (item --) --)
def each
def each ([T] (T --) --)
over len each-len! # Get total length
0 each-idx! # index
(
Expand All @@ -20,13 +19,7 @@ def each
end

# map (list quote -- list)
def map
# (
# [T*]: list
# (T -- U): q
# --
# [U*]
# )
def map ([T] (T -- U) -- [U])
over len map-len! # Get total length
0 map-idx! # Index
[] map-accum! # Accumulator
Expand All @@ -45,7 +38,7 @@ def map
end

# filter (list quote -- list)
def filter
def filter ([T] (T -- bool) -- [T])
over len filter-len! # Get total length
0 filter-idx! # Index
[] filter-accum! # Accumulator
Expand All @@ -64,7 +57,7 @@ def filter
end

# foldl (quote initial list -- result)
def foldl
def foldl ((T T -- T) T [T] -- T)
# quote initial list
swap foldl-accum! # Accumulator,
# quote list
Expand All @@ -80,24 +73,24 @@ def foldl
end

# sum (list -- value)
def sum
def sum ([float] -- float)
(+) 0 rot foldl
end

# .. (-- list[str]), equals kinda looks like lines.
def .. stdin lines end
def .. (-- [str]) stdin lines end

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

# wt = Whitespace separated Table (-- list[list[str]])
def wt .. (wsplit) map end
# wt = Whitespace separated Table (-- [[str*]*])
def wt (-- [[str]]) .. (wsplit) map end

# wjoin = Whitespace join (list -- str)
def wjoin " " join end
# wjoin = Whitespace join ([str*] -- str)
def wjoin ([str] -- str) " " join end

# unlines (list[str] -- str)
def unlines
def unlines ([str] -- str)
[] unlines-accum! # Accumulator
(
@unlines-accum append
Expand All @@ -107,8 +100,8 @@ def unlines
@unlines-accum "" join
end

# reverse (list|string -- list)
def reverse
# reverse ([T*]|str -- [T*]|str)
def reverse ([T] -- [T])
dup len 1 - reverse-idx! # Set Idx
[] # list accum
(
Expand All @@ -120,16 +113,15 @@ def reverse
end

# abs (int|float -- int|float)
def abs
def abs (float -- float)
[(dup 0 <) (-1 *)] if
end


def tab " " end
def tsplit tab split end
def uw unlines w end
def tab (-- str) " " end
def tsplit (str -- [str]) tab split end
def uw ([str] --) unlines w end

# readTsvFile (str -- list[list[str]])
def readTsvFile
def readTsvFile (str -- [[str]])
readFile lines (tsplit) map end
end
11 changes: 10 additions & 1 deletion mshell/Lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"unicode"
"encoding/json"
)

type TokenType int
Expand Down Expand Up @@ -62,6 +63,7 @@ const (
// TYPESTRING, using str token instead
TYPEBOOL
DOUBLEDASH
AMPERSAND
)

func (t TokenType) String() string {
Expand Down Expand Up @@ -170,6 +172,8 @@ func (t TokenType) String() string {
return "TYPEBOOL"
case DOUBLEDASH:
return "DOUBLEDASH"
case AMPERSAND:
return "AMPERSAND"
default:
return "UNKNOWN"
}
Expand All @@ -185,7 +189,8 @@ type Token struct {
}

func (t Token) ToJson() string {
return fmt.Sprintf("{\"line\": %d, \"column\": %d, \"start\": %d, \"lexeme\": \"%s\", \"type\": \"%s\"}", t.Line, t.Column, t.Start, t.Lexeme, t.Type)
escaped, _ := json.Marshal(t.Lexeme)
return fmt.Sprintf("{\"line\": %d, \"column\": %d, \"start\": %d, \"lexeme\": %s, \"type\": \"%s\"}", t.Line, t.Column, t.Start, string(escaped), t.Type)
}

func (t Token) DebugString() string {
Expand Down Expand Up @@ -268,6 +273,8 @@ var notAllowedLiteralChars = map[rune]bool{
'!': true,
'@': true,
'=': true,
'&': true,
'|': true,
}

func isAllowedLiteral(r rune) bool {
Expand Down Expand Up @@ -447,6 +454,8 @@ func (l *Lexer) scanToken() Token {
return l.parseLiteralOrKeyword()
case '=':
return l.makeToken(EQUALS)
case '&':
return l.makeToken(AMPERSAND)
case '<':
if l.peek() == '=' {
l.advance()
Expand Down
8 changes: 6 additions & 2 deletions mshell/MShellObject.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strconv"
"strings"
"encoding/json"
)

type Jsonable interface {
Expand Down Expand Up @@ -752,7 +753,8 @@ func (obj *MShellSimple) Slice(startInc int, endExc int) (MShellObject, error) {

// ToJson
func (obj *MShellLiteral) ToJson() string {
return fmt.Sprintf("{\"type\": \"Literal\", \"value\": \"%s\"}", obj.LiteralText)
escBytes, _ := json.Marshal(obj.LiteralText)
return fmt.Sprintf("{\"type\": \"Literal\", \"value\": \"%s\"}", string(escBytes))
}

func (obj *MShellBool) ToJson() string {
Expand Down Expand Up @@ -788,7 +790,9 @@ func (obj *MShellList) ToJson() string {
}

func (obj *MShellString) ToJson() string {
return fmt.Sprintf("{\"type\": \"String\", \"content\": \"%s\"}", obj.Content)
// Escape the content
escBytes, _ := json.Marshal(obj.Content)
return fmt.Sprintf("{\"type\": \"String\", \"content\": %s}", string(escBytes))
}

func (obj *MShellPipe) ToJson() string {
Expand Down
Loading

0 comments on commit 634a66d

Please sign in to comment.