Skip to content

Commit

Permalink
Merge pull request #227 from NeowayLabs/add-var
Browse files Browse the repository at this point in the history
add 'var' keyword.
  • Loading branch information
i4ki authored Mar 5, 2018
2 parents 09e8c98 + 5d77218 commit 28aecce
Show file tree
Hide file tree
Showing 46 changed files with 5,813 additions and 4,881 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ build:
cd cmd/nashfmt && go build $(buildargs)
cd stdbin/mkdir && go build $(buildargs)
cd stdbin/pwd && go build $(buildargs)
cd stdbin/write && go build $(buildargs)
cd stdbin/strings && go build $(buildargs)

NASHPATH=$(HOME)/nash
Expand All @@ -29,6 +30,7 @@ install: build
cp -pr ./stdlib $(NASHROOT)/stdlib
cp -pr ./stdbin/mkdir/mkdir $(NASHROOT)/bin/mkdir
cp -pr ./stdbin/pwd/pwd $(NASHROOT)/bin/pwd
cp -pr ./stdbin/write/write $(NASHROOT)/bin/write
cp -pr ./stdbin/strings/strings $(NASHROOT)/bin/strings

docsdeps:
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ supports output redirection to tcp, udp and unix network protocols
To assign command output to a variable exists the '<=' operator. See the example
below:
```sh
fullpath <= realpath $path | xargs -n echo
var fullpath <= realpath $path | xargs -n echo
echo $fullpath
```
The symbol '<=' redirects the stdout of the command or function invocation in the
Expand All @@ -142,8 +142,8 @@ If you want the command output splited into an array, then you'll need
to store it in a temporary variable and then use the builtin `split` function.

```sh
out <= find .
files <= split($out, "\n")
var out <= find .
var files <= split($out, "\n")

for f in $files {
echo "File: " + $f
Expand All @@ -157,7 +157,7 @@ enclosing every variable with quotes before executing the command.
Then the following example do the right thing:
```sh
fullname = "John Nash"
var fullname = "John Nash"
./ci-register --name $fullname --option somevalue
```
On bash you need to enclose the `$fullname` variable in quotes to avoid problems.
Expand Down Expand Up @@ -211,7 +211,7 @@ Long commands can be split in multiple lines:
λ> (aws ec2 attach-internet-gateway --internet-gateway-id $igwid
--vpc-id $vpcid)
λ> instanceId <= (
λ> var instanceId <= (
aws ec2 run-instances
--image-id ami-xxxxxxxx
--count 1
Expand Down
64 changes: 64 additions & 0 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,24 @@ type (
elseTree *Tree
}

// VarAssignDeclNode is a "var" declaration to assign values
VarAssignDeclNode struct {
NodeType
token.FileInfo
egalitarian

Assign *AssignNode
}

// VarExecAssignDeclNode is a var declaration to assign output of fn/cmd
VarExecAssignDeclNode struct {
NodeType
token.FileInfo
egalitarian

ExecAssign *ExecAssignNode
}

// FnArgNode represents function arguments
FnArgNode struct {
NodeType
Expand Down Expand Up @@ -377,6 +395,12 @@ const (

NodeFnArg

// NodeVarAssignDecl is the type for var declaration of values
NodeVarAssignDecl

// NodeVarExecAssignDecl
NodeVarExecAssignDecl

// NodeFnDecl is the type for function declaration
NodeFnDecl

Expand Down Expand Up @@ -1131,6 +1155,46 @@ func (a *FnArgNode) IsEqual(other Node) bool {
return true
}

func NewVarAssignDecl(info token.FileInfo, assignNode *AssignNode) *VarAssignDeclNode {
return &VarAssignDeclNode{
NodeType: NodeVarAssignDecl,
Assign: assignNode,
}
}

func (n *VarAssignDeclNode) IsEqual(other Node) bool {
if !n.equal(n, other) {
return false
}

o, ok := other.(*VarAssignDeclNode)
if !ok {
return false
}

return n.Assign.IsEqual(o.Assign)
}

func NewVarExecAssignDecl(info token.FileInfo, assignNode *ExecAssignNode) *VarExecAssignDeclNode {
return &VarExecAssignDeclNode{
NodeType: NodeVarExecAssignDecl,
ExecAssign: assignNode,
}
}

func (n *VarExecAssignDeclNode) IsEqual(other Node) bool {
if !n.equal(n, other) {
return false
}

o, ok := other.(*VarExecAssignDeclNode)
if !ok {
return false
}

return n.ExecAssign.IsEqual(o.ExecAssign)
}

// NewFnDeclNode creates a new function declaration
func NewFnDeclNode(info token.FileInfo, name string) *FnDeclNode {
return &FnDeclNode{
Expand Down
3 changes: 3 additions & 0 deletions ast/node_fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,9 @@ func (n *IfNode) String() string {
return ifStr
}

func (n *VarAssignDeclNode) String() string { return "var " + n.Assign.String() }
func (n *VarExecAssignDeclNode) String() string { return "var " + n.ExecAssign.String() }

// String returns the string representation of function declaration
func (n *FnDeclNode) String() string {
fnStr := "fn"
Expand Down
4 changes: 2 additions & 2 deletions ast/nodetype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ when it is executed, like:
```nash
echo
echo "acessing individual parameter"
somearg = $ARGS[0]
var somearg = $ARGS[0]
echo $somearg
echo
```
Expand All @@ -53,8 +53,8 @@ To branch you can use **if** statement, it requires
a boolean expression, like the comparison operator:

```nash
a = "nash"
echo $a
var a = "nash"
echo -n $a
if $a == "nash" {
a = "rocks"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/append.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env nash

example_list = ()
var example_list = ()
echo "appending string 1"
example_list <= append($example_list, "1")
echo $example_list
Expand Down
14 changes: 6 additions & 8 deletions examples/args.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/usr/bin/env nash

echo "iterating through the arguments list"
echo ""
print("iterating through the arguments list\n\n")
for arg in $ARGS {
echo $arg
print("%s\n", $arg)
}

echo ""
echo "acessing individual parameter"
somearg = $ARGS[0]
echo $somearg
echo ""
print("\n")
print("acessing individual parameter\n")
var somearg = $ARGS[0]
print("%s\n", $somearg)
30 changes: 16 additions & 14 deletions examples/init
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
#!/usr/bin/env nash

# Simple init script for you base your own
# For a more complete and organized .nash see:
# https://github.com/tiago4orion/dotnash

# PROMPT is a special variable used by nash command line to
# setup your prompt.
RED = "\033[31m"
GREEN = "\033[32m"
RESET = "\033[0m"

PROMPTSYM = "λ> "
DEFPROMPT = $RED + $PROMPTSYM + $RESET

PROMPT = $DEFPROMPT
var RED = ""
var GREEN = ""
var RESET = ""
var PROMPTSYM = "λ> "
var DEFPROMPT = $RED+$PROMPTSYM+$RESET

setenv PROMPT
setenv PROMPT = $DEFPROMPT

# cd overrides built-in cd
# Add the current branch before prompt (if current directory is a git repo)
fn cd(path) {
var branch = ""
var prompt = ""

if $path == "" {
path = $HOME
}

chdir($path)

-test -d ./.git
var _, status <= test -d ./.git

if $status != "0" {
PROMPT = $DEFPROMPT
prompt = $DEFPROMPT
} else {
branch <= git rev-parse --abbrev-ref HEAD | xargs echo -n
PROMPT = "(" + $GREEN + $branch + $RESET + ")" + $DEFPROMPT

prompt = "("+$GREEN+$branch+$RESET+")"+$DEFPROMPT
}

setenv PROMPT
setenv PROMPT = $prompt
}

bindfn cd cd

# syntax sugar to cd into go project
fn gocd(path) {
cd $GOPATH + "/src/" + $path
cd $GOPATH+"/src/"+$path
}

bindfn gocd gocd
4 changes: 2 additions & 2 deletions hack/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ done

$GO tool cover -func coverage.txt

echo "running stdlib tests"
echo "running stdlib and stdbin tests"
export NASHPATH=`pwd`
tests=$(find ./stdlib -name "*_test.sh")
tests=$(find ./stdlib ./stdbin -name "*_test.sh")

for t in ${tests[*]}
do
Expand Down
8 changes: 4 additions & 4 deletions internal/sh/builtin/append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestAppend(t *testing.T) {
},
{
name: "simple append",
code: `a = ()
code: `var a = ()
a <= append($a, "hello")
a <= append($a, "world")
echo -n $a...`,
Expand All @@ -73,7 +73,7 @@ func TestAppend(t *testing.T) {
},
{
name: "append is for lists",
code: `a = "something"
code: `var a = "something"
a <= append($a, "other")
echo -n $a...`,
expectedErr: "<interactive>:2:8: append expects a " +
Expand All @@ -83,15 +83,15 @@ func TestAppend(t *testing.T) {
},
{
name: "var args",
code: `a <= append((), "1", "2", "3", "4", "5", "6")
code: `var a <= append((), "1", "2", "3", "4", "5", "6")
echo -n $a...`,
expectedErr: "",
expectedStdout: "1 2 3 4 5 6",
expectedStderr: "",
},
{
name: "append of lists",
code: `a <= append((), (), ())
code: `var a <= append((), (), ())
if len($a) != "2" {
print("wrong")
} else if len($a[0]) != "0" {
Expand Down
3 changes: 3 additions & 0 deletions internal/sh/builtin/exit_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package builtin_test

import (
"os"
"os/exec"
"testing"
)
Expand Down Expand Up @@ -41,6 +42,8 @@ func TestExit(t *testing.T) {
for name, desc := range tests {
t.Run(name, func(t *testing.T) {
cmd := exec.Command(projectnash, desc.script, desc.status)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr // to know why scripts were failing
err := cmd.Run()
if err == nil {
if desc.status == "0" {
Expand Down
Loading

0 comments on commit 28aecce

Please sign in to comment.