Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some web functionality + go coroutines #159

Merged
merged 17 commits into from
May 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ piston_meta = "0.26.1"
range = "0.3.1"
rand = "0.3.13"
read_color = "0.1.0"
hyper = "0.9.4"
18 changes: 12 additions & 6 deletions assets/syntax.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ _seps: "(){}[],.:;=<>*·+-/%^?|&∧∨!¬∑∃∀\n"
202 w = .r!({.w! comment})

0 fn = {
["fn" .w! .."("!:"name" ?w "(" ?w args ?w ")" ?w ?["->":"returns" ?w ?type:"ret_type"] ?w block:"block"]
["fn" .w! .."("!:"name" ?w "(" ?w args ?w ")" ?w {
["->":"returns" ?w ?type:"ret_type"]
!"->":!"returns"
} ?w block:"block"]
[.."("!:"name" ?w "(" ?w args ?w ")" ?w "=" ?w expr:"expr"]
}
1 args = .s?.(, arg:"arg")
Expand Down Expand Up @@ -84,10 +87,11 @@ _seps: "(){}[],.:;=<>*·+-/%^?|&∧∨!¬∑∃∀\n"
25 call = [.._seps!:"name" ?w "(" .s?.(, arg_expr:"call_arg") ?w ")"]
26 named_call = [.._seps!:"word" ?w "(" ?w
.s?.(, [.._seps!:"word" ?w ":" ?w arg_expr:"call_arg" ?w]) ")"]
27 assign = [lexpr:"left" ?w assign_op ?w expr:"right"]
28 assign_op = {":=":":=" "=":"=" "+=":"+=" "-=":"-=" "*=":"*=" "/=":"/=" "%=":"%="}
29 compare = [lexpr:"left" ?w compare_op ?w expr:"right"]
30 compare_op = {"==":"==" "!=":"!=" "¬=":"!=" "<=":"<=" "<":"<" ">=":">=" ">":">"}
27 go = ["go" ?w {call:"call" named_call:"named_call"}]
28 assign = [lexpr:"left" ?w assign_op ?w expr:"right"]
29 assign_op = {":=":":=" "=":"=" "+=":"+=" "-=":"-=" "*=":"*=" "/=":"/=" "%=":"%="}
30 compare = [lexpr:"left" ?w compare_op ?w expr:"right"]
31 compare_op = {"==":"==" "!=":"!=" "¬=":"!=" "<=":"<=" "<":"<" ">=":">=" ">":">"}

40 label = ?["'" .._seps!:"label" ?w ":" ?w]
41 short_body = [.w! .._seps!:"name" ?w {["[" ?w expr:"start" , expr:"end" ?w ")"]
Expand All @@ -96,7 +100,7 @@ _seps: "(){}[],.:;=<>*·+-/%^?|&∧∨!¬∑∃∀\n"
43 , = [?w "," ?w]
44 arr = {array:"array" array_fill:"array_fill"}
45 items = {vec4:"vec4" ["(" ?w expr ?w ")"] unop:"unop"
text call:"call" named_call:"named_call"
text go:"go" call:"call" named_call:"named_call"
num bool color item:"item"}

50 short_loops = {sum:"sum" min:"min" max:"max" sift:"sift" any:"any" all:"all"}
Expand All @@ -119,6 +123,8 @@ _seps: "(){}[],.:;=<>*·+-/%^?|&∧∨!¬∑∃∀\n"
"[]":"arr_any"
["[" ?w type:"arr" ?w "]"]
"{}":"obj_any"
["thr" ?w "[" ?w type:"thr" ?w "]"]
"thr":"thr_any"
}

101 + = [?w {"+":"+" "||":"+" "∨":"+" "or":"+"} ?w]
Expand Down
7 changes: 7 additions & 0 deletions editor-plugins/atom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ Dyon syntax highlighting for the Atom editor
3. Create a new folder `~/.atom/packages/language-dyon`
4. Copy the content of `editor-plugins/atom/` into the new folder
5. Restart Atom

Alternative to restart:

1. In Atom, go to the menu `Packages -> Command Palette -> Toggle`
2. Type "Window: Reload" and hit Enter

The window will flash for a moment and reload the packages.
4 changes: 2 additions & 2 deletions editor-plugins/atom/grammars/dyon.cson
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
'core_types': {
'comment': 'Built-in/core type'
'name': 'storage.type.core.dyon'
'match': '\\b(bool|f64|vec4|str|opt|res)\\b'
'match': '\\b(bool|f64|vec4|str|opt|res|thr)\\b'
}
'core_vars': {
'comment': 'Core type variant'
Expand Down Expand Up @@ -140,7 +140,7 @@
{
'comment': 'Keyword'
'name': 'keyword.other.dyon'
'match': '\\b(sum|min|max|any|all|sift|and|or)\\b'
'match': '\\b(sum|min|max|any|all|sift|and|or|go)\\b'
}
{ 'include': '#self' }
{ 'include': '#mut' }
Expand Down
24 changes: 17 additions & 7 deletions interactive/examples/common/functions.dyon
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,36 @@ fn print_function(f: {}) {
print(f.name)
print("(")
n := len(f.arguments)
for i := 0; i < n; i += 1 {
for i n {
print(f.arguments[i].name)
if f.arguments[i].lifetime != none() {
print(": '" + unwrap(f.arguments[i].lifetime))
if f.arguments[i].takes != "any" {
print(" " + f.argument[i].takes)
}
} else {
if f.arguments[i].takes != "any" {
print(": " + f.arguments[i].takes)
}
}
if (i + 1) < n {
print(", ")
}
}
print(")")
if f.returns {
print(" ->")
if f.returns != "void" {
if f.returns == "any" {
print(" ->")
} else {
print(" -> " + f.returns)
}
}
println("")
}

fn print_functions(functions: [{}]) {
n := len(functions)
for i := 0; i < n; i += 1 {
if functions[i].type != "external" { continue }
fn print_functions_type(functions: [{}], type) {
for i len(functions) {
if functions[i].type != type { continue }
print_function(functions[i])
}
}
10 changes: 1 addition & 9 deletions interactive/examples/piston_window/loader.dyon
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@ fn main() {
println("Reset with R.")
println("You can modify \"source/piston_window/snake.rs\" while running.")

{
println("External functions:")
println("===================")
functions := unwrap(load("examples/common/functions.dyon"))
list := functions()
call(functions, "print_functions", [list])
println("===================")
}

render := unwrap(load("examples/piston_window/render.dyon"))
source := "examples/piston_window/snake.dyon"
m := unwrap(load(source: source, imports: [render]))
call(m, "init", [])

settings := call_ret(m, "settings", [])
data := call_ret(m, "init_data", [settings])
Expand Down
14 changes: 14 additions & 0 deletions interactive/examples/piston_window/snake.dyon
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
title() = "Snake!"

fn init() {
functions := unwrap(load("examples/common/functions.dyon"))
list := functions()
println("===================")
println("External functions:")
println("===================")
call(functions, "print_functions_type", [list, "external"])
println("===================")
println("Loaded functions:")
println("===================")
call(functions, "print_functions_type", [list, "loaded"])
println("===================")
}

settings() = {
background_color: #e5d6a1,
reload_interval: 0.25,
Expand Down
15 changes: 15 additions & 0 deletions source/bench/threads_go.dyon
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sum_n(i: f64, n: f64) = ∑ j n { i * n + j + 1 }

fn results(mut threads: [thr[f64]]) -> res[f64] {
return ok(∑ _ len(threads) {
join(thread: pop(mut threads))?
})
}

fn main() {
n := 100_000

t := 2
threads := sift i t { go sum_n(i, n / t) }
x := unwrap(results(mut threads))
}
6 changes: 6 additions & 0 deletions source/bench/threads_no_go.dyon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sum_n(i: f64, n: f64) = ∑ j n { i * n + j + 1 }

fn main() {
n := 100_000
x := sum_n(0, n)
}
17 changes: 14 additions & 3 deletions source/functions/functions.dyon
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,29 @@ fn print_function(f) {
print(f.name)
print("(")
n := len(f.arguments)
for i := 0; i < n; i += 1 {
for i n {
print(f.arguments[i].name)
if f.arguments[i].lifetime != none() {
print(": '" + unwrap(f.arguments[i].lifetime))
if f.arguments[i].takes != "any" {
print(" " + f.argument[i].takes)
}
} else {
if f.arguments[i].takes != "any" {
print(": " + f.arguments[i].takes)
}
}
if (i + 1) < n {
print(", ")
}
}
print(")")
if f.returns {
print(" ->")
if f.returns != "void" {
if f.returns == "any" {
print(" ->")
} else {
print(" -> " + f.returns)
}
}
print(" ... ")
print(f.type)
Expand Down
13 changes: 13 additions & 0 deletions source/syntax/lifetime_9.dyon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn foo(a: 'b f64, mut b: []) -> {
b[0] = {a: a}
debug()
return 5
}

fn main() {
y := 5
x := [{}, 2, 3]
foo_thread := go foo(y, mut x)
r := unwrap(join(thread: foo_thread))
println("r: " + to_string(r))
}
2 changes: 1 addition & 1 deletion source/syntax/new_pos.dyon
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn new() {
{x: 0, y: 0}
_ := {x: 0, y: 0}
}
17 changes: 15 additions & 2 deletions source/test.dyon
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
fn main() {
data := unwrap(load(meta: "assets/test_meta.txt", file: "assets/test_data.txt"))
println(data)
list := [
"https://raw.githubusercontent.com/PistonDevelopers/dyon/master/assets/test_data.txt",
"https://raw.githubusercontent.com/PistonDevelopers/dyon/master/assets/test_meta.txt"
]
names := [
"1.txt",
"2.txt"
]
threads := sift i len(list) {
go download(url: list[i], file: names[i])
}
results := sift i len(threads) {
unwrap(join(thread: pop(mut threads)))
}
println(results)
}
11 changes: 11 additions & 0 deletions source/typechk/go.dyon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn bar() {}

fn main() {
data_thread := go bar()
for i 10 {
sleep(0.1)
}
println("Done here")
data := unwrap(join(thread: data_thread))
println(data)
}
17 changes: 17 additions & 0 deletions source/typechk/threads.dyon
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fn sum_n(i: f64, n: f64) -> f64 {
return ∑ j n { i * n + j + 1 }
}

fn results(mut threads: [f64]) -> res[f64] {
return ok(∑ _ len(threads) {
join(thread: pop(mut threads))?
})
}

fn main() {
n := 100

t := 2
threads := sift i t { go sum_n(i, n / t) }
println(unwrap(results(mut threads)))
}
8 changes: 8 additions & 0 deletions source/typechk/unused_result.dyon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn foo() -> f64 {
3 + 4
return 7
}

fn main() {
println(foo())
}
7 changes: 7 additions & 0 deletions source/typechk/unused_result_2.dyon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn foo() {
3 + 4
}

fn main() {
foo()
}
Loading