Skip to content

Commit

Permalink
Imports example
Browse files Browse the repository at this point in the history
* Fix imports
* Minor doc update
  • Loading branch information
algorandskiy committed Dec 6, 2019
1 parent b3c0d20 commit 865b271
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
12 changes: 6 additions & 6 deletions GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ In opposite, expression is evaluated to some value.

## Declarations, definitions and assignments

Constants, variables and function are supported:
Constants, variables and functions are supported:
```
const a = 1
const b = "abc\x01"
Expand Down Expand Up @@ -133,10 +133,10 @@ There are 4 builtin objects: `txn`, `gtxn`, `global`, `args`. Accessing them is

| Object and Syntax | Description |
| --- | --- |
| `args[N]` | returns Args[N] value |
| `txn.FIELD` | retrieves field from current transaction |
| `args[N]` | returns Args[N] value as []byte |
| `txn.FIELD` | retrieves field from current transaction (see below) |
| `gtxn[N].FIELD` | retrieves field from a transaction N in the current transaction group |
| `global.FIELD` | returns globals |
| `global.FIELD` | returns globals (see below) |

#### Transaction fields
| Index | Name | Type | Notes |
Expand All @@ -146,8 +146,8 @@ There are 4 builtin objects: `txn`, `gtxn`, `global`, `args`. Accessing them is
| 2 | FirstValid | uint64 | round number |
| 3 | FirstValidTime | uint64 | Causes program to fail; reserved for future use. |
| 4 | LastValid | uint64 | round number |
| 5 | Note | []byte | |
| 6 | Lease | []byte | |
| 5 | Note | []byte | Note field |
| 6 | Lease | []byte | Lease field |
| 7 | Receiver | []byte | 32 byte address |
| 8 | Amount | uint64 | micro-Algos |
| 9 | CloseRemainderTo | []byte | 32 byte address |
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ make java-gui
1. Constant folding.
2. Strings concatenation.
3. Fix known limitations.
4. Code generation for return at the end of function
4. Code generation for return at the end of function.
5. Improve errors reporting.

## Limitations

Expand Down
21 changes: 15 additions & 6 deletions compiler/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compiler
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"

Expand All @@ -23,7 +24,7 @@ func resolveModule(moduleName string, sourceDir string, currentDir string) (Inpu
sourceDir = currentDir
} else {
components := strings.Split(moduleName, ".")
locations := make([]string, 16)
locations := make([]string, 0, 16)

// search relative to source file first
fullPath := path.Join(sourceDir, path.Join(components...))
Expand All @@ -37,15 +38,15 @@ func resolveModule(moduleName string, sourceDir string, currentDir string) (Inpu

for _, loc := range locations {
if fileExists(loc) {
sourceFile = path.Base(fullPath)
sourceDir = path.Dir(fullPath)
srcBytes, err := ioutil.ReadFile(fullPath)
sourceFile = path.Base(loc)
sourceDir = path.Dir(loc)
srcBytes, err := ioutil.ReadFile(loc)
if err != nil {
return InputDesc{}, err
}
source = string(srcBytes)
source = string(srcBytes) + "\n"
break
}
break
}

if source == "" {
Expand All @@ -54,3 +55,11 @@ func resolveModule(moduleName string, sourceDir string, currentDir string) (Inpu
}
return InputDesc{source, sourceFile, sourceDir, currentDir}, nil
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
14 changes: 5 additions & 9 deletions compiler/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"runtime/debug"

"github.com/antlr/antlr4/runtime/Go/antlr"
Expand Down Expand Up @@ -719,6 +718,10 @@ func (l *exprListener) EnterFunCall(ctx *gen.FunCallContext) {
funCallExprNode := l.funCallEnterImpl(name, argExprNodes)
listener := newTreeNodeListener(l.ctx, funCallExprNode)
info.parser(listener, funCallExprNode)
if listener.node == nil {
reportError("function parsing failed", parser, token, rule)
return
}
defNode := listener.node.(*funDefNode)

if err != nil {
Expand Down Expand Up @@ -874,14 +877,6 @@ type InputDesc struct {
CurrentDir string
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

func parseModule(moduleName string, parseCtx *parseContext, parent TreeNodeIf, ctx *context) (TreeNodeIf, error) {
resolver := resolveModule
if parseCtx.moduleResolver != nil {
Expand All @@ -905,6 +900,7 @@ func parseModule(moduleName string, parseCtx *parseContext, parent TreeNodeIf, c

collector.filterAmbiguity()
if len(collector.errors) > 0 {
parseCtx.collector.copyErrors(collector)
return nil, fmt.Errorf("error during module %s parsing", moduleName)
}

Expand Down
10 changes: 10 additions & 0 deletions examples/imports.tl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import stdlib.templates
import mymodule

function logic() {
let to = addr"PNWOET7LLOWMBMLE4KOCELCX6X3D3Q4H2Q4QJASYIEOF7YIPPQBG3YQ5YI"
let amount = 2000
let l = getLease()
let result = DynamicFee(to, amount, global.ZeroAddress, 1, 1000, l)
return result
}
3 changes: 3 additions & 0 deletions examples/mymodule.tl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function getLease() {
return b64"dBlHI6BdrIg="
}

0 comments on commit 865b271

Please sign in to comment.