Skip to content

Commit

Permalink
Added operational atoms and newMessage/newResult procedures, still ne…
Browse files Browse the repository at this point in the history
…eds testing
  • Loading branch information
TheDevtop committed Jan 17, 2025
1 parent 37ba1ce commit 0e4c9df
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion libsexp.nimble
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Package
version = "2.0.0"
version = "2.1.0"
author = "Thijs Haker"
description = "S-Expression library"
license = "MIT"
Expand Down
17 changes: 11 additions & 6 deletions src/libsexp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ const tokFalse: string = ":false"
const tokOk: string = ":ok"
const tokErr: string = ":err"

# Operational atoms
const AtomOk*: Atom = tokOk
const AtomErr*: Atom = tokErr

# Construct cell
type Cons* = tuple[car: Exp, cdr: List]

# Convert a list to construct cell
proc toCons*(list: List): Cons =
if len(list) < 1:
return (car: Exp(tag: tagAtom, valAtom: tokErr), cdr: @[])
return (car: Exp(tag: tagAtom, valAtom: AtomErr), cdr: @[])
elif len(list) == 1:
return (car: list[0], cdr: @[])
return (car: list[0], cdr: list[1..^1])
Expand Down Expand Up @@ -78,11 +82,11 @@ proc unmapQuotes*(input: string): string =
return input
return strutils.strip(s = input, chars = {'"'})

# Allocate error -> (:err "Content to error")
proc newError*(mesg: string): Exp = Exp(tag: tagList, valList: @[Exp(tag: tagAtom, valAtom: tokErr), Exp(tag: tagString, valString: mapQuotes(mesg))])
# Allocate message -> (:err "Something went wrong")
proc newMessage*(atom: Atom, mesg: string): Exp = Exp(tag: tagList, valList: @[Exp(tag: tagAtom, valAtom: atom), Exp(tag: tagString, valString: mapQuotes(mesg))])

# Allocate ok value -> (:ok exp)
proc newOk*(exp: Exp): Exp = Exp(tag: tagList, valList: @[Exp(tag: tagAtom, valAtom: tokOk), exp])
# Allocate result -> (:ok 10), (:err :false)
proc newResult*(atom: Atom, exp: Exp): Exp = Exp(tag: tagList, valList: @[Exp(tag: tagAtom, valAtom: atom), exp])

# Allocate new list from variadic expressions
proc newList*(exps: varargs[Exp]): List =
Expand All @@ -93,6 +97,7 @@ proc newList*(exps: varargs[Exp]): List =

# Match input with s-expression regex, return matching tokens
proc lex(input: string): seq[string] =
# Compiled regular expression
const crex: regex.Regex2 = regex.re2("(\"[^\"]*\"|\\(|\\)|\"|[^\\s()\"]+)")
var tokens: seq[string]

Expand Down Expand Up @@ -146,7 +151,7 @@ proc parse(tokens: seq[string]): Exp =

# If nothing was parsed, return error
if ret.len() == 0:
return newError("Parsed empty input string")
return newMessage(AtomErr, "Parsed empty input string")
return ret[0]

# Decode strings to s-expressions
Expand Down
10 changes: 0 additions & 10 deletions tests/testLibsexp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ test "Test newList()":
test "Test mapQuotes() and unmapQuotes()":
check mapQuotes("1 2 3") == "\"1 2 3\"" and unmapQuotes("\"3 2 1\"") == "3 2 1"

test "Test encoded newOk()":
let goodOp = newOk(Exp(tag: tagInt, valInt: 10))
let output = encode(goodOp)
check output == "(:ok 10)"

test "Test encoded newError()":
let badOp = newError("Get rekt!")
let output = encode(badOp)
check output == "(:err \"Get rekt!\")"

test "Test int, float":
const input = "(42 3.14)"
let output = libsexp.decode(input)
Expand Down

0 comments on commit 0e4c9df

Please sign in to comment.