Skip to content

Commit

Permalink
added nil literal
Browse files Browse the repository at this point in the history
  • Loading branch information
Jomy10 committed Dec 24, 2022
1 parent 634387d commit ae3e3b0
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ The interpreter is therefore focussed on readability rather than speed.
- [x] integers
- [ ] **strings**
- [ ] **floats**
- [ ] **booleans**
- [ ] structs
- [ ] interfaces
- **Functions**
- [x] functions
- [ ] functions as variables
- [ ] **scopes**
- [ ] modules
- **Statements**
- **if/elsif/else**
- **match**

## Native Function Interface

Expand Down
3 changes: 2 additions & 1 deletion interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func Interpret(nodes []parser.Node, stdout, stderr io.Writer, stdin io.Reader) e
for _, node := range nodes {
_, err := ExecNode(&runtime, node)
if err != nil {
fmt.Printf("[Runtime error] %v\n", err)
return err
}
}
Expand Down Expand Up @@ -46,6 +45,8 @@ func ExecNode(runtime *runtime.Runtime, node parser.Node) (interface{}, error) {
return newFunction(runtime, node.(parser.FunctionDeclNode))
case parser.ReturnNode:
return ExecNode(runtime, node.(parser.ReturnNode).Expr)
case parser.NilLiteralNode:
return nil, nil
}
return nil, errors.New(fmt.Sprintf("Noot error: Invalid node `%#v`", node))
}
Expand Down
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

import (
"fmt"
"github.com/jomy10/nootlang/interpreter"
"github.com/jomy10/nootlang/parser"
"os"
)

func main() {
tokens, err := parser.Tokenize("def add(a, b) { return a + b; }\n noot!(add(1, 2))")
tokens, err := parser.Tokenize("noot!(nil)")
if err != nil {
panic(err)
}
Expand All @@ -16,5 +17,7 @@ func main() {
panic(err)
}

interpreter.Interpret(nodes, os.Stdout, os.Stderr, os.Stdin)
if err := interpreter.Interpret(nodes, os.Stdout, os.Stderr, os.Stdin); err != nil {
panic(fmt.Sprintf("[Runtime error] %v\n", err))
}
}
1 change: 1 addition & 0 deletions nootish/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Interactive shell for nootlang
2 changes: 2 additions & 0 deletions parser/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ type FunctionDeclNode struct {
type ReturnNode struct {
Expr Node
}

type NilLiteralNode struct{}
8 changes: 2 additions & 6 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func parseExpression(tokenIter Iterator[Token]) (Node, error) {
default:
return parseBinaryExpression(tokenIter)
}
case Nil:
return NilLiteralNode{}, nil
default:
return nil, errors.New(fmt.Sprintf("Invalid start of expression `%v`", firstToken))
}
Expand Down Expand Up @@ -166,11 +168,6 @@ func parseFunctionCallArguments(tokenIter Iterator[Token]) ([]Node, error) {

var args []Node
for _, arg := range argList {
// fmt.Printf("Parsing arg %v\n", arg)
fmt.Println("Parsing arg")
for _, a := range arg {
fmt.Printf("%v ", *a)
}
argIter := newArrayOfPointerIterator(arg)
expr, err := parseExpression(&argIter)
if err != nil {
Expand Down Expand Up @@ -276,7 +273,6 @@ func collectList(tokenIter Iterator[Token], closingToken TT) ([][]*Token, error)
idx := 0
for true {
nextToken, hasNext := tokenIter.next()
fmt.Printf("> %v\n", nextToken)

if !hasNext {
if closingToken == EOS {
Expand Down
8 changes: 8 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ func TestParseListNested(t *testing.T) {
}
}

func TestParseNil(t *testing.T) {
source := "a := nil"
expected := []Node{
VarDeclNode{"a", NilLiteralNode{}},
}
testParsing(source, expected, t)
}

func testParsing(source string, expected []Node, t *testing.T) {
tokens, err := Tokenize(source)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions parser/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
Comma // ,
Def // def
Return // return
Nil // nil
)

// A single token
Expand Down Expand Up @@ -62,6 +63,7 @@ func Tokenize(source string) ([]Token, error) {
{Comma, regexp.MustCompile(`\A(,)`)},
{Def, regexp.MustCompile(`\A(def)`)},
{Return, regexp.MustCompile(`\A(return)`)},
{Nil, regexp.MustCompile(`\A(nil)`)},
{Ident, regexp.MustCompile(`\A(\w|!)+`)},
}

Expand Down
7 changes: 7 additions & 0 deletions parser/tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func TestFunction(t *testing.T) {
testTokenizing(source, expected, t)
}

func TestNil(t *testing.T) {
source := "nil"
expected := []Token{{Nil, "nil"}}

testTokenizing(source, expected, t)
}

func testTokenizing(source string, expected []Token, t *testing.T) {
tokens, err := Tokenize(source)

Expand Down

0 comments on commit ae3e3b0

Please sign in to comment.