Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2895d7c
file to try out Encore, and preliminary tests for for-comprehension
ElieOaks Apr 3, 2019
957edcc
Changes to parser to handle the new for-comprehension, toplevel to ad…
ElieOaks Apr 24, 2019
ce50299
added funcion to array, further dev. of optimizer, mutable boxes incl…
ElieOaks May 8, 2019
97e2549
removed scrap files beofre review PR
ElieOaks May 8, 2019
61c15fa
removed translation of for
ElieOaks May 8, 2019
979bcb6
changed the names om mutable boxed file names
ElieOaks May 8, 2019
8b61939
removed the old named files from git
ElieOaks May 8, 2019
067051d
added requirement for functor
ElieOaks May 8, 2019
4cb0e56
removed old commented out for-parsing
ElieOaks May 8, 2019
17a835d
finally got to the boxing problem
ElieOaks May 16, 2019
8a365c9
added another file: TypedDesugaring, which is another pass of the com…
ElieOaks May 20, 2019
c5204b8
lots of stuff
ElieOaks Jun 3, 2019
0c100a5
for-comprehension works
ElieOaks Jun 16, 2019
e2b1f0b
break works
ElieOaks Jun 22, 2019
af9141b
writing test cases
ElieOaks Jun 26, 2019
17f1b6b
begnning the clean up of code
ElieOaks Jun 26, 2019
499d158
cleaned up code and worked off of Kiko's comments
ElieOaks Jun 28, 2019
ddce7ab
removed stray file
ElieOaks Jun 28, 2019
8a3c951
removed stray file
ElieOaks Jun 28, 2019
5dbc4a9
fixed a space
ElieOaks Jun 28, 2019
ba10b39
fixed a space
ElieOaks Jun 28, 2019
308149e
fixed a space
ElieOaks Jun 28, 2019
740946b
fixed a space
ElieOaks Jun 28, 2019
2c13220
fixed a space
ElieOaks Jun 28, 2019
9edd6c4
fixed a space
ElieOaks Jun 28, 2019
b475d04
smaller changes
ElieOaks Jun 28, 2019
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 encore.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ executable encorec
, Makefile
, ModuleExpander
, Optimizer.Optimizer
, Optimizer.TypedDesugarer
, Parser.Parser
, SystemUtils
, Typechecker.Capturechecker
Expand Down
10 changes: 10 additions & 0 deletions modules/standard/Boxed/MutBox.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module MutBox

local class MutBox[t]

var value : t

def init( v : t) : unit
this.value = v
end
end
5 changes: 3 additions & 2 deletions modules/standard/Collections/Mutable/ArrayList.enc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import Data.Maybe(cat_maybes)
import Data.Either
import Data.Array
import Collections.Mutable.Collection
import Collections.Mutable.Functor

local class ArrayList[t] : Collection[t](next_empty, int_arr, foreach(), shift_right(), shift_left(), ensure_can_accomodate(), resize())
local class ArrayList[t] : Collection[t](next_empty, int_arr, foreach(), shift_right(), shift_left(), ensure_can_accomodate(), resize()) + Functor[t](map(), flatMap())
var int_arr : [Maybe[t]]
var next_empty : uint

Expand Down Expand Up @@ -151,7 +152,7 @@ local class ArrayList[t] : Collection[t](next_empty, int_arr, foreach(), shift_r
val clone = new ArrayList[t]()

for x <- this.int_arr do
match x with
match x with
case Just(ice) => clone.append(ice)
case Nothing => { break; () }
end
Expand Down
8 changes: 8 additions & 0 deletions modules/standard/Collections/Mutable/Functor.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Functor
import Collections.Mutable.Collection

local trait Functor[t]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A functor, as defined in Haskell, is only your map operation.
We are not using Haskell concepts, though, so feel free to keep it as is.
This is just a FYI :)

require def map[u](f : local ((t) -> u)) : Functor[u]
require def flatMap[u](f : local ((t) -> Functor[u])) : Collection[u]
require def foreach(f : local ((t) -> unit)) : unit
end
8 changes: 4 additions & 4 deletions modules/standard/Collections/Mutable/HashMap.enc
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ local class HashMapIterator[k : Hashable + Eq[k], v]
this.current_index = this.current_index + 1

while this.current_index < this.map.size do
val l = (this.map.internal_map)(this.current_index)
val l = (this.map.internal_map)(this.current_index)
if l.size > 0 then
return l.first
end

this.current_index = this.current_index + 1
end

Expand Down Expand Up @@ -107,7 +107,7 @@ local class HashMap[k : Hashable + Eq[k], v] : Map[k, v](size, internal_map, ite
def init() : unit
this.items = 0
this.size = 32
this.internal_map = Array.new_with_generator(this.size,
this.internal_map = Array.new_with_generator(this.size,
fun (x: int) => new LinkedList[Entry[k,v]]())
end

Expand Down Expand Up @@ -277,7 +277,7 @@ local class HashMap[k : Hashable + Eq[k], v] : Map[k, v](size, internal_map, ite
end
result
end

def populate(pairs : [(k, v)]) : unit
for kv <- pairs do
this.set(kv.0, kv.1)
Expand Down
10 changes: 6 additions & 4 deletions modules/standard/Collections/Mutable/LinkedList.enc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module LinkedList
import Data.Either
import Data.Maybe
import Collections.Mutable.Collection
-- import Collections.Mutable.Iterable

import Collections.Mutable.Functor

local class LinkedNode[t] : Id
var value : t
Expand All @@ -16,7 +17,7 @@ local class LinkedNode[t] : Id
end
end

local class LinkedList[t] : Collection[t](drop(), first, last, size) + Id
local class LinkedList[t] : Collection[t](drop(), first, last, size) + Id + Functor[t](first, map(), flatMap(), foreach())
var first : Maybe[LinkedNode[t]]
var last : Maybe[LinkedNode[t]]
var size : int
Expand Down Expand Up @@ -325,7 +326,7 @@ local class LinkedList[t] : Collection[t](drop(), first, last, size) + Id
end
end

def map[u](f : t -> u) : LinkedList[u]
def map[u](f : local ((t) -> u)) : LinkedList[u]
val result = new LinkedList[u]()

-- Iterate over list, perform f() on each value, put result in new list.
Expand Down Expand Up @@ -358,7 +359,8 @@ local class LinkedList[t] : Collection[t](drop(), first, last, size) + Id
return result
end

def flatMap[u](f : t -> LinkedList[u]) : LinkedList[u]

def flatMap[u](f : local ((t) -> Functor[u])) : LinkedList[u]
val result = new LinkedList[u]()

var cursor = this.first
Expand Down
79 changes: 70 additions & 9 deletions modules/standard/Data/Array.enc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Array

-- new_with_default :: (int, a) -> [a]
-- new_with_default(size, default) creates an array of size default
-- with each element
-- with each element
fun new_with_default[a](size : int, default : a) : [a]
val arr = new [a](size)
for i <- [0 .. size - 1] do
Expand Down Expand Up @@ -72,8 +72,8 @@ end
-- count(pred, arr) counts the number of elements of arr satisfying predicate pred
fun count[a](pred : a -> bool, arr : [a]) : int
var count = 0
for a <- arr do
if pred(a) then
for x <- arr do
if pred(x) then
count += 1
end
end
Expand All @@ -83,14 +83,76 @@ end
-- map :: (a -> b, [a]) -> [b]
-- map(f, arr) produces a new array containing the results of applying f to the
-- elements of arr
fun map[a,b](f : a -> b, arr : [a]) : [b]
fun map[a,b](f : local ((a) -> b), arr : [a]) : [b]
val ret = new [b](|arr|)
for i <- [0 .. |arr|-1] do
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this work with for-comprehensions?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather avoid using for-comprehension inside of map, flatMap and foreach, due to the potential endless compilation loop it could result in. Unless I used it with a range or some other kind of data strucutre. But even then, for performance sake, I would rather use a while, or repeat loop inside of these methods.

var i = 0
while (i <= |arr|-1) do
ret(i) = f((arr)(i))
i += 1
end
ret
end

-- flatMap :: (a -> [b], [a]) -> [b]
-- flatMap(f, arr) produces a new array containeing the flatened results of applying f to
-- the elements of arr
fun flatMap[a, b](f : local ((a) -> [b]), arr : [a]) : [b]
val size = |arr|
var result = new [b](0)
var i = 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this work with for-comprehensions?

while (i < size) do
var ret = f(arr(i))
result = concat[b](result, ret)
i += 1
end
return result
end

-- foreach(a -> unit, [a]) -> unit
-- forach(f, arr) applied f to all elements of an array, resulting in unit.
fun foreach[a](f : local ((a) -> unit), arr : [a]) : unit
val size = |arr|
var i = 0
while i < size do
f(arr(i))
i += 1
end
end


fun maybeForeach[a](f : local ((a) -> Maybe[unit]), arr : [a]) : Maybe[unit]
val size = |arr|
var i = 0
while i < size do
var res = f(arr(i))
if res == Nothing then
return Nothing
end
i += 1
end
return Just(())
end

-- concat :: ([a], [a]) -> [a]
-- concat(firstArr, secondArr) produces a new array that is the concatination of
-- the two input arrays.
fun concat[a](firstArr : [a], secondArr : [a]) : [a]
val firstSize = |firstArr|
val secondSize = |secondArr|
val result = new [a](firstSize + secondSize)
var i = 0
while i < firstSize do
result(i) = firstArr(i)
i += 1
end
var j = 0
while j < secondSize do
result(j+firstSize) = secondArr(j)
j += 1
end
return result
end

-- show :: (a -> unit, [a]) -> unit
-- show(showEl, arr) prints out array arr using function showEl to print the elements
-- of the array
Expand Down Expand Up @@ -123,7 +185,7 @@ fun contains[t](arr : [t], to_find : t) : bool
end

-- contains_str :: ([String], String) -> bool
-- contains_str(arr, elem) is true if and only if elem appears in arr
-- contains_str(arr, elem) is true if and only if elem appears in arr
-- tested using String.compare
fun contains_str(arr : [String], to_find : String) : bool
var retval = false
Expand All @@ -147,7 +209,7 @@ fun clone[t](src : [t]) : [t]
end

-- nclone :: ([t], int) -> Maybe[[t]]
-- nclone(arr,n) results in Just(arr') where arr' is a new array containing
-- nclone(arr,n) results in Just(arr') where arr' is a new array containing
-- the first n elements of arr, in the case where n < |arr|, otherwise Nothing
-- a new array containing the same contents as arr
fun nclone[t](src : [t], n : uint) : Maybe[[t]]
Expand All @@ -160,5 +222,4 @@ fun nclone[t](src : [t], n : uint) : Maybe[[t]]
end
Just(new_arr)
end
end

end
21 changes: 10 additions & 11 deletions modules/standard/Data/Maybe.enc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module Maybe

import qualified Data.Array as A

-- This package implements common functions for operating on
-- This package implements common functions for operating on
-- Maybe types. It is inspired heavily by Data.Maybe in Haskell.

-- unjust :: Maybe[a] -> a
-- unjust(mval) eliminates the Just constructor, assuming that the
-- unjust :: Maybe[a] -> a
-- unjust(mval) eliminates the Just constructor, assuming that the
-- mval is Just(x). Results in an error otherwise
fun unjust[a](mval : Maybe[a]) : a
match mval with
Expand All @@ -18,8 +18,8 @@ fun unjust[a](mval : Maybe[a]) : a
end
end

-- unjust_with_default :: (a, Maybe[a]) -> a
-- unjust_with_default(default, mval) eliminates the Just constructor from mval,
-- unjust_with_default :: (a, Maybe[a]) -> a
-- unjust_with_default(default, mval) eliminates the Just constructor from mval,
-- assuming that the input value is Just(x). Results in an default, otherwise
fun unjust_with_default[a](default : a, mval : Maybe[a]) : a
maybe(default, id[a], mval)
Expand Down Expand Up @@ -54,7 +54,7 @@ fun is_just[a](mval : Maybe[a]) : bool
case Nothing => false
end
end

-- is_nothing :: Maybe[a] -> bool
-- is_nothing(mval) is true whenever mval is Nothing
fun is_nothing[a](mval : Maybe[a]) : bool
Expand Down Expand Up @@ -91,19 +91,18 @@ end
fun cat_maybes[a](marr : [Maybe[a]]) : [a]
val arr = new [a](A.count(is_just[a], marr)) -- why do I need [a]?
var count = 0
for a <- marr do
if is_just(a) then
arr(count) = unjust(a)
for b <- marr do
if is_just(b) then
arr(count) = unjust(b)
count += 1
end
end
arr
end

-- map_maybe :: (a -> Maybe[a], [a]) -> [b]
-- map_maybe(f, arr) maps the function f on the array producing a new array,
-- map_maybe(f, arr) maps the function f on the array producing a new array,
-- filtering out elements that result in Nothing
fun map_maybe[a,b](f : a -> Maybe[b], arr : [a]) : [b]
cat_maybes[b](A.map[a, Maybe[b]](f, arr))
end

33 changes: 33 additions & 0 deletions modules/standard/Data/RRange.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module RRange

read class RRange
val start : int
val stop : int
val step : int

def init(start : int, stop : int, step : int) : unit
this.start = start
this.stop = stop
this.step = step
end

def foreach(f : local ((int) -> unit)) : unit
var current = this.start
while (current <= this.stop) do
f(current)
current += this.step
end
end

def maybeForeach(f : local ((int) -> Maybe[unit])) : Maybe[unit]
var current = this.start
while (current <= this.stop) do
var ret = f(current)
if ret == Nothing then
return Nothing
end
current += this.step
end
return Just(())
end
end
1 change: 1 addition & 0 deletions src/back/CCode/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class UsableAs a b where
instance UsableAs Name Lval where
instance UsableAs Lval Expr where
instance UsableAs Name Expr where

instance UsableAs a a where

instance UsableAs Stat Expr where
Expand Down
1 change: 0 additions & 1 deletion src/back/CodeGen/Closure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import Types as Ty

import Control.Monad.State hiding (void)
import Control.Arrow(first)
import Debug.Trace

varSubFromTypeVars :: [Type] -> [(ID.Name, CCode Lval)]
varSubFromTypeVars = map each
Expand Down
Loading