Skip to content

Commit

Permalink
Fix variable declaration syntax
Browse files Browse the repository at this point in the history
Previous syntax of `boi: varname boi` was objectively wrong.

Syntax has been changed to `ONE VARNAME BOI`
  • Loading branch information
KernelDeimos committed Oct 29, 2017
1 parent 6cd75f5 commit 3ead1de
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
7 changes: 7 additions & 0 deletions demo.boi
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ bloop < boi:tmp [int 10] boi
boi: tmp [+ boi:tmp [int 1]] boi
BOI

ONE CHANGEY BOI
boi? cat true boi
ONE CHANGEY BOI
boi, "value is " boi:CHANGEY boi
BOI
boi, "value is " boi:CHANGEY boi

24 changes: 24 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"crypto/rand"
"errors"
"fmt"
)
Expand Down Expand Up @@ -75,6 +76,29 @@ func BoiFuncSet(context *BoiContext, args []BoiVar) (BoiVar, error) {
return args[1], nil
}

// BoiFuncDeclare is similar to BoiFuncSet, but does not require a value
// parameter. It instead initializes the variable to a completely random
// value to **ensure** the application programmer can't make assumptions
// about the value. Adding a value parameter anyway is undefined behaviour.
func BoiFuncDeclare(context *BoiContext, args []BoiVar) (BoiVar, error) {
if len(args) < 1 {
return BoiVar{}, errors.New("one requires 1 parameters")
}
key := string(args[0].data)
//context.parentCtx.variables[key] = args[1]

value := make([]byte, 4)
_, err := rand.Read(value)
if err != nil {
return BoiVar{}, err
}

// We can't use .Set() here, because that tries to find the variable
// in parent scopes.
context.parentCtx.variables[key] = BoiVar{value}
return BoiVar{value}, nil
}

func BoiFuncCat(context *BoiContext, args []BoiVar) (BoiVar, error) {
output := []byte{}
for _, arg := range args {
Expand Down
18 changes: 17 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ func NewBoiInterpreter(input []byte) *BoiInterpreter {
boi.context.functions["dec"] = BoiFuncDec{boi}
boi.RegisterGoFunction("<", BoiFuncLess)

// Grey area (memes, also practical)
boi.RegisterGoFunction("declare", BoiFuncDeclare)

// Memes
boi.RegisterGoFunction("IsEven", BoiFuncIsEven)

Expand Down Expand Up @@ -309,6 +312,18 @@ func (boi *BoiInterpreter) getStatement() (*BoiStatement, error) {

return NewCallStatement("set", tokens), nil

case "one":
fallthrough
case "ONE":
boi.pos += 4
boi.noeof(boi.whitespace())
tokens, err := boi.GetTokens()
if err != nil {
return nil, err
}

return NewCallStatement("declare", tokens), nil

case "boi?":
boi.pos += 4
boi.noeof(boi.whitespace())
Expand Down Expand Up @@ -451,7 +466,8 @@ func (boi *BoiInterpreter) eatToken() (Token, error) {
}

keyword := string(boi.rSyntaxToken.Find(boi.input[boi.pos:]))
isBoi := keyword == "boi" || keyword == "]" || keyword == ";"
isBoi := keyword == "boi" || keyword == "]" || keyword == ";" ||
keyword == "BOI"
if isBoi {
boi.pos += IntyBoi(len(keyword))
t := Token{
Expand Down

0 comments on commit 3ead1de

Please sign in to comment.