Skip to content

Commit

Permalink
update branch abs-source-file with changes from 1.2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ntwrick committed Feb 18, 2019
2 parents 5edaccc + 1f52898 commit 5b89547
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 22 deletions.
76 changes: 76 additions & 0 deletions CODE_OF_CONDUCT.md
@@ -0,0 +1,76 @@
# Contributor Covenant Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, sex characteristics, gender identity and expression,
level of experience, education, socio-economic status, nationality, personal
appearance, race, religion, or sexual identity and orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at alessandro.nadalin@gmail.com. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq
41 changes: 37 additions & 4 deletions docs/syntax/system-commands.md
Expand Up @@ -40,6 +40,41 @@ if `ls -la`.ok {
}
```
## Executing commands in background
Sometimes you might want to execute a command in
background, so that the script keeps executing
while the command is running. In order to do so,
you can simply add an `&` at the end of your script:
``` bash
`sleep 10 &`
echo("This will be printed right away!")
```
You might also want to check whether a command
is "done", by checking the boolean `.done` property:
``` bash
cmd = `sleep 10 &`
cmd.done # false
`sleep 11`
cmd.done # true
```
If, at some point, you want to wait for the command
to finish before running additional code, you can
use the `wait` method:
``` bash
cmd = `sleep 10 &`
echo("This will be printed right away!")
cmd.wait()
echo("This will be printed after 10s")
```
## Interpolation
You can also replace parts of the command with variables
declared within your program using the `$` symbol:
Expand All @@ -57,6 +92,8 @@ $(echo $PWD) # "" since the ABS variable PWD doesn't exist
$(echo \$PWD) # "/go/src/github.com/abs-lang/abs"
```
## Limitations
Currently, commands that use the `$()` syntax need to be
on their own line, meaning that you will not
be able to have additional code on the same line.
Expand All @@ -69,10 +106,6 @@ $(sleep 10); echo("hello world")
Note that this is currently a limitation that will likely
be removed in the future (see [#41](https://github.com/abs-lang/abs/issues/41)).
Commands are blocking and cannot be run in parallel, although
we're planning to support background execution in the future
(see [#70](https://github.com/abs-lang/abs/issues/70)).
Also note that, currently, the implementation of system commands
requires the `bash` executable to [be available on the system](https://github.com/abs-lang/abs/blob/5b5b0abf3115a5dd4dfe8485501f8765985ad0db/evaluator/evaluator.go#L696-L722).
On Windows, commands are executed through [cmd.exe](https://github.com/abs-lang/abs/blob/ee793641be09ad8572c3e913fef8468f69b0c0a2/evaluator/evaluator.go#L1101-L1103).
Expand Down
86 changes: 74 additions & 12 deletions evaluator/evaluator.go
Expand Up @@ -19,10 +19,10 @@ import (
)

var (
NULL = &object.Null{}
EOF = &object.Error{Message: "EOF"}
TRUE = &object.Boolean{Value: true}
FALSE = &object.Boolean{Value: false}
NULL = object.NULL
EOF = object.EOF
TRUE = object.TRUE
FALSE = object.FALSE
Fns map[string]*object.Builtin
)

Expand Down Expand Up @@ -56,7 +56,6 @@ func BeginEval(program ast.Node, env *object.Environment, lexer *lexer.Lexer) ob
}

func Eval(node ast.Node, env *object.Environment) object.Object {

switch node := node.(type) {
// Statements
case *ast.Program:
Expand Down Expand Up @@ -921,6 +920,14 @@ func evalPropertyExpression(pe *ast.PropertyExpression, env *object.Environment)
return obj.Ok
}

return FALSE
}
// Special .done property of commands
if pe.Property.String() == "done" {
if obj.Done != nil {
return obj.Done
}

return FALSE
}
case *object.Hash:
Expand Down Expand Up @@ -1073,6 +1080,7 @@ func evalHashIndexExpression(tok token.Token, hash, index object.Object) object.
}

func evalCommandExpression(tok token.Token, cmd string, env *object.Environment) object.Object {
cmd = strings.Trim(cmd, " ")
// Match all strings preceded by
// a $ or a \$
re := regexp.MustCompile("(\\\\)?\\$([a-zA-Z_]{1,})")
Expand Down Expand Up @@ -1100,6 +1108,18 @@ func evalCommandExpression(tok token.Token, cmd string, env *object.Environment)
return v.Inspect()
})

// A background command ends with a '&'
background := len(cmd) > 1 && cmd[len(cmd)-1] == '&'
// If this is a background command
// we'll remove the trailing '&' and
// execute it in background ourselves
if background {
cmd = cmd[:len(cmd)-2]
}

// The string holding the command
s := &object.String{}

// thanks to @haifenghuang
var commands []string
var executor string
Expand All @@ -1112,16 +1132,58 @@ func evalCommandExpression(tok token.Token, cmd string, env *object.Environment)
}
c := exec.Command(executor, commands...)
c.Env = os.Environ()
var out bytes.Buffer
var stderr bytes.Buffer
c.Stdin = os.Stdin
c.Stdout = &out
var stdout bytes.Buffer
var stderr bytes.Buffer
c.Stdout = &stdout
c.Stderr = &stderr
err := c.Run()

s.Stdout = &stdout
s.Stderr = &stderr
s.Cmd = c
s.Token = tok

var err error
if background {
// If we want to run the command in background,
// let's set it as running. With this, others can
// wait for it by calling s.Wait().
s.SetRunning()
go evalCommandInBackground(c, s)
} else {
err = c.Run()
}

if !background {
if err != nil {
s.SetCmdResult(FALSE)
} else {
s.SetCmdResult(TRUE)
}
}

return s
}

// Runs a background command.
// We will start it, set its result
// and then mark it as done, so that
// callers stuck on s.Wait() can resume.
func evalCommandInBackground(c *exec.Cmd, s *object.String) {
defer s.SetDone()
err := c.Start()

if err != nil {
s.SetCmdResult(FALSE)
return
}

err = c.Wait()

if err != nil {
return &object.String{Token: tok, Value: stderr.String(), Ok: FALSE}
s.SetCmdResult(FALSE)
return
}
// trim space at both ends of out.String(); works in both linux and windows
return &object.String{Token: tok, Value: strings.TrimSpace(out.String()), Ok: TRUE}

s.SetCmdResult(TRUE)
}
9 changes: 9 additions & 0 deletions evaluator/evaluator_test.go
Expand Up @@ -800,6 +800,8 @@ c")`, []string{"a", "b", "c"}},
{`1.66.ceil()`, 2},
{`"1.23".ceil()`, 2},
{`"1.66".ceil()`, 2},
{`sleep(0.01)`, nil},
{`$()`, ""},
}
for _, tt := range tests {
evaluated := testEval(tt.input)
Expand Down Expand Up @@ -1131,6 +1133,13 @@ func TestCommand(t *testing.T) {
{"`echo -n hello world`", "hello world"},
{"`echo hello world | xargs echo -n`", "hello world"},
{"`echo \\$CONTEXT`", "abs"},
{"`sleep 0.01`", ""},
{"`sleep 0.01`.done", true},
{"`sleep 0.01`.ok", true},
{"`sleep 0.01 &`", ""},
{"`sleep 0.01 &`.done", false},
{"`sleep 0.01 &`.ok", false},
{"`sleep 0.01 &`.wait().ok", false},
}
}
for _, tt := range tests {
Expand Down
22 changes: 22 additions & 0 deletions evaluator/functions.go
Expand Up @@ -232,6 +232,11 @@ func getFns() map[string]*object.Builtin {
Types: []string{object.STRING_OBJ},
Fn: upperFn,
},
// wait(`sleep 1 &`)
"wait": &object.Builtin{
Types: []string{object.STRING_OBJ},
Fn: waitFn,
},
// trim("abc")
"trim": &object.Builtin{
Types: []string{object.STRING_OBJ},
Expand Down Expand Up @@ -1139,6 +1144,23 @@ func upperFn(tok token.Token, args ...object.Object) object.Object {
return &object.String{Token: tok, Value: strings.ToUpper(args[0].(*object.String).Value)}
}

// wait(`sleep 10 &`)
func waitFn(tok token.Token, args ...object.Object) object.Object {
err := validateArgs(tok, "wait", args, 1, [][]string{{object.STRING_OBJ}})
if err != nil {
return err
}

cmd := args[0].(*object.String)

if cmd.Cmd == nil {
return cmd
}

cmd.Wait()
return cmd
}

// trim("abc")
func trimFn(tok token.Token, args ...object.Object) object.Object {
err := validateArgs(tok, "trim", args, 1, [][]string{{object.STRING_OBJ}})
Expand Down
1 change: 1 addition & 0 deletions examples/background-command-exit.abs
@@ -0,0 +1 @@
cmd = `sleep 10 &`
33 changes: 33 additions & 0 deletions examples/background-command.abs
@@ -0,0 +1,33 @@
# Simple background command
cmd = `sleep 1; ls -la &`
echo("Started")
sleep(1100)
echo("finished")
echo(cmd)
echo(cmd.ok)

# Simple background command, we get the output by sleeping
cmd = `sleep 1; ls -la &`
echo("Started")
sleep(1100)
echo("finished")
echo(cmd)
echo(cmd.ok)

# Background command wait
cmd = `sleep 1; ls -la &`
echo("Started")
cmd.wait()
echo("finished")
echo(cmd)
echo(cmd.ok)

# Background command wait with error
cmd = `sleep 1; ls la &`
echo("Started")
cmd.wait()
echo("finished")
echo(cmd)
echo(cmd.ok)

# TODO make cmd.wait() work

0 comments on commit 5b89547

Please sign in to comment.