Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add lib/std/sequence.ab

Fixe a bug of clsref definition.
  • Loading branch information...
commit 3c8424b78ccea517173b58354266abf1230ddb3a 1 parent 91e5a52
@nineties authored
View
52 lib/amber/syntax.ab
@@ -2,7 +2,7 @@ Assign{Qualified{Syntax,comment}, Qualified{Syntax,shell_style_comment}}
# Copyright (C) 2010 nineties
#
-# $Id: syntax.ab 2013-03-19 03:11:17 nineties $
+# $Id: syntax.ab 2013-03-24 18:57:01 nineties $
# Syntax definition of the Amber language.
# This file will be loaded first.
@@ -375,6 +375,40 @@ ReserveSymbol{not, and, or, when, open, if, else, case, of,
}
## Syntax Sugars
+
+rewrite_operator_assign(lhs, rhs, op): {
+ split: (Subscript{lhs, args}) -> {
+ stmt: []
+ new_args: []
+ new_lhs: lhs
+
+ for (a in args) {
+ if (head(a) != \Symbol) {
+ t: genSym()
+ stmt = cons(`DefineVariable{!t, !a}, stmt)
+ new_args = cons(t, new_args)
+ } else
+ new_args = cons(a, new_args)
+ }
+ new_args = reverse(new_args)
+
+ if (head(lhs) != \Symbol) {
+ t: genSym()
+ stmt = cons(`DefineVariable{!t, !lhs}, stmt)
+ new_lhs = t
+ }
+
+ (`Subscript{!new_lhs, !new_args}, `Seq{!stmt})
+ }
+ | x -> (x, nil)
+
+ (x, s): split(lhs)
+ `{
+ !s
+ !x = (!op)(!x, !rhs)
+ }
+}
+
+x => `uplus(!x)
-x => `uminus(!x)
x + y => `add(!x, !y)
@@ -385,14 +419,14 @@ x / y => `div(!x, !y)
x % y => `mod(!x, !y)
x << y => `shiftL(!x, !y)
x >> y => `shiftR(!x, !y)
-x += y => `(!x = add(!x, !y))
-x -= y => `(!x = sub(!x, !y))
-x *= y => `(!x = mul(!x, !y))
-x ^= y => `(!x = pow(!x, !y))
-x /= y => `(!x = div(!x, !y))
-x %= y => `(!x = mod(!x, !y))
-x <<= y => `(!x = shiftL(!x, !y))
-x >>= y => `(!x = shiftR(!x, !y))
+x += y => rewrite_operator_assign(x, y, \add)
+x -= y => rewrite_operator_assign(x, y, \sub)
+x *= y => rewrite_operator_assign(x, y, \mul)
+x ^= y => rewrite_operator_assign(x, y, \pow)
+x /= y => rewrite_operator_assign(x, y, \div)
+x %= y => rewrite_operator_assign(x, y, \mod)
+x <<= y => rewrite_operator_assign(x, y, \shiftL)
+x >>= y => rewrite_operator_assign(x, y, \shiftR)
x < y => `lt(!x, !y)
x > y => `gt(!x, !y)
x <= y => `le(!x, !y)
View
12 lib/std/array.ab
@@ -1,8 +1,18 @@
# Copyright (C) 2012 nineties
#
-# $Id: array.ab 2012-12-16 01:53:20 nineties $
+# $Id: array.ab 2013-03-23 22:56:00 nineties $
module Std {
+ add(ar1@Array, ar2@Array): append(ar1, ar2)
+
+ index(ar@Array, v): {
+ for (i in 0..ar.size-1) {
+ if (v == ar[i])
+ return i
+ }
+ return undef
+ }
+
each(ary@Array): {
i: 0
n: ary.size
View
8 lib/std/random.ab
@@ -1,6 +1,6 @@
# Copyright (C) 2012 nineties
#
-# $Id: random.ab 2013-03-21 19:34:16 nineties $
+# $Id: random.ab 2013-03-24 10:38:02 nineties $
module Std {
module impl {
@@ -37,11 +37,11 @@ module Std {
uniform_distribution(n): make UniformDistribution{ n }
discrete_distribution(seq): {
seq = seq.to_array
- make DiscreteDistribution{ seq.to_array, seq.sum }
+ make DiscreteDistribution{ seq, seq.sum }
}
- rand(UniformDistrib{n}): rand(n)
- rand(DiscreteDistrib{ws, total}): {
+ rand(UniformDistribution{n}): rand(n)
+ rand(DiscreteDistribution{ws, total}): {
w: 0
r: rand(total)
for (i in 0..ws.size-1) {
View
8 lib/std/sequence.ab
@@ -0,0 +1,8 @@
+# Copyright (C) 2012 nineties
+#
+# $Id: sequence.ab 2013-03-23 22:56:54 nineties $
+
+module Std {
+ sum(seq): foldl(add, 0, seq)
+ product(seq): foldl(mul, 1, seq)
+}
View
15 rowl1/rowl1-compile.rlc
@@ -2,7 +2,7 @@
; rowl - 1st generation
; Copyright (C) 2010 nineties
;
-; $Id: rowl1-compile.rlc 2013-03-23 22:42:24 nineties $
+; $Id: rowl1-compile.rlc 2013-03-25 13:09:14 nineties $
;
(import "rlvm-compile")
@@ -267,12 +267,17 @@
(node_arg expr 0)
(alpha tbl (node_arg expr 1))))
)
- (if (|| (== hd DefVariable) (== hd DefFunction)) (do
+ (if (== hd DefVariable) (do
(= expr (_copy expr))
(node_arg_set expr 1 (alpha tbl (node_arg expr 1)))
(node_arg_set expr 0 (alpha_args tbl (node_arg expr 0)))
(return expr)
))
+ (if (== hd DefFunction) (do
+ (= expr (_copy expr))
+ (node_arg_set expr 1 (alpha tbl (node_arg expr 1)))
+ (return expr)
+ ))
(if (|| (== hd Block) (== hd Breakable)) (do
(symtable_push tbl)
(var exprs (node_arg_list expr 0))
@@ -2031,9 +2036,9 @@
)
(return (compile_define_variable asm mod
- (make_object3 DefVariable
- lhs fun (to_sym "overwrite")
- )))
+ (make_object2 DefVariable
+ lhs fun)
+ ))
))
(fun append_function (mod sym newfunc) (
View
3  rowl1/rowl1-interp.rlc
@@ -2,7 +2,7 @@
; rowl - 1st generation
; Copyright (C) 2010 nineties
;
-; $Id: rowl1-interp.rlc 2013-03-21 03:07:39 nineties $
+; $Id: rowl1-interp.rlc 2013-03-23 22:57:06 nineties $
;
(import "rlvm-compile")
@@ -96,6 +96,7 @@
(import_file "std/numeric.ab")
(import_file "std/format.ab")
(import_file "std/list.ab")
+ (import_file "std/sequence.ab")
(import_file "std/array.ab")
(import_file "std/table.ab")
(import_file "std/oop.ab")
Please sign in to comment.
Something went wrong with that request. Please try again.