Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
16 changes: 16 additions & 0 deletions modules/standard/Boxed/ImmutableBox/Bool.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- This file was automatically converted by encorec

module Bool

read class Bool
val value : bool
def init(x : bool) : unit
this.value = x
end
def show() : String
string_from_bool(this.value)
end
def value() : bool
this.value
end
end
20 changes: 20 additions & 0 deletions modules/standard/Boxed/MutableBox/MutBool.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- This file was automatically converted by encorec

module MutBool

local class MutBool
var value : bool

def init(x : bool) : unit
this.value = x
end

def show() : String
string_from_bool(this.value)
end

def value() : bool
this.value
end

end
20 changes: 20 additions & 0 deletions modules/standard/Boxed/MutableBox/MutChar.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- This file was automatically converted by encorec

module MutChar

local class MutChar
var value : char

def init(x : char) : unit
this.value = x
end

def show() : String
string_from_char(this.value)
end

def value() : char
this.value
end

end
20 changes: 20 additions & 0 deletions modules/standard/Boxed/MutableBox/MutInteger.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- This file was automatically converted by encorec

module MutInteger

local class MutInteger
var value : int

def init(x : int) : unit
this.value = x
end

def show() : String
string_from_int(this.value)
end

def value() : int
this.value
end

end
20 changes: 20 additions & 0 deletions modules/standard/Boxed/MutableBox/MutReal.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- This file was automatically converted by encorec

module MutReal

local class MutReal
var value : real

def init(x : real) : unit
this.value = x
end

def show() : String
string_from_real(this.value)
end

def value() : real
this.value
end

end
20 changes: 20 additions & 0 deletions modules/standard/Boxed/MutableBox/MutString.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- This file was automatically converted by encorec

module MutString

local class MutString
var value : String

def init(x : String) : unit
this.value = x
end

def show() : String
this.value
end

def value() : String
this.value
end

end
14 changes: 14 additions & 0 deletions modules/standard/Boxed/MutableBox/MutUnit.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

module MutUnit

local class MutUnit

def show() : String
"()"
end

def value() : unit
()
end

end
8 changes: 5 additions & 3 deletions modules/standard/Collections/Mutable/ArrayList.enc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import Data.Maybe(cat_maybes)
import Data.Either
import Data.Array
import Collections.Mutable.Collection
-- JOY for-comprehension
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 +153,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 Expand Up @@ -304,7 +306,7 @@ local class ArrayList[t] : Collection[t](next_empty, int_arr, foreach(), shift_r

return result
end

def underlying() : [t]
cat_maybes(this.int_arr)
end
Expand Down
10 changes: 10 additions & 0 deletions modules/standard/Collections/Mutable/Functor.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- JOY for-comprehension
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.

It is nice to have the name, but I would remove this line :)

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 : t -> u) : Collection[u]
require def flatMap[u](f : t -> Functor[u]) : Collection[u]
require def foreach[u](f : 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
4 changes: 3 additions & 1 deletion modules/standard/Collections/Mutable/LinkedList.enc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Data.Either
import Data.Maybe
import Collections.Mutable.Collection
-- import Collections.Mutable.Iterable
-- JOY for-comprehension
import Collections.Mutable.Functor

local class LinkedNode[t] : Id
var value : t
Expand All @@ -16,7 +18,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](map(), flatMap())
var first : Maybe[LinkedNode[t]]
var last : Maybe[LinkedNode[t]]
var size : int
Expand Down
52 changes: 48 additions & 4 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 @@ -91,6 +91,51 @@ fun map[a,b](f : a -> b, arr : [a]) : [b]
ret
end

-- flatMap :: (a -> [b], [a]) -> [b]
-- flatMap(f, arr) produces a new array containeing the results of applying f to
-- the elements of arr
-- This function is necessary for for-loops to work. Do not change without testing!
-- TODO: write tests for these!!
fun flatMap[a, b](f : a -> [b], arr : [a]) : [b]
val size = |arr|
var ret = new [b](0)
repeat i <- size do
var result = f(arr(i-1))
ret = concat(ret, result)
end

return ret
end

-- foreach(a -> unit, [a]) -> unit
-- forach(f, arr) applied f to all elements of an array, without expecting a result.
-- This function is necessary for for-loops to work. Do not change without testing!
-- TODO: write tests for these!!
fun foreach[a](f : a -> unit, arr : [a]) : unit
val size = |arr|
repeat i <- size do
f(arr(i-1))
end
end

-- concat :: ([a], [a]) -> [a]
-- concat(firstArr, secondArr) produces a new array that is the concatination of
-- the two input arrays.
-- TODO: write tests for these!
fun concat[a](firstArr : [a], secondArr : [a]) : [a]
val firstSize = |firstArr|
val secondSize = |secondArr|
val ret = new [a](firstSize + secondSize)
repeat i <- firstSize do
ret(i-1) = firstArr(i-1)
end
repeat i <- secondSize do
ret(i-1+firstSize) = secondArr(i-1)
end
return ret
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 +168,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 +192,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 @@ -161,4 +206,3 @@ fun nclone[t](src : [t], n : uint) : Maybe[[t]]
Just(new_arr)
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
71 changes: 0 additions & 71 deletions src/back/CodeGen/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -677,77 +677,6 @@ instance Translatable A.Expr (State Ctx.Context (CCode Lval, CCode Stat)) where
(_,tbody) <- translate body
return (unit, While (StatAsExpr ncond tcond) (Statement tbody))

translate for@(A.For {A.name, A.step, A.src, A.body}) = do
indexVar <- Var <$> Ctx.genNamedSym "index"
eltVar <- Var <$> Ctx.genNamedSym (show name)
startVar <- Var <$> Ctx.genNamedSym "start"
stopVar <- Var <$> Ctx.genNamedSym "stop"
stepVar <- Var <$> Ctx.genNamedSym "step"
srcStepVar <- Var <$> Ctx.genNamedSym "src_step"

(srcN, srcT) <- if A.isRangeLiteral src
then return (undefined, Comm "Range not generated")
else translate src

let srcType = A.getType src
eltType = if Ty.isRangeType srcType
then int
else translate $ Ty.getResultType (A.getType src)
srcStart = if Ty.isRangeType srcType
then Call rangeStart [srcN]
else Int 0 -- Arrays start at 0
srcStop = if Ty.isRangeType srcType
then Call rangeStop [srcN]
else BinOp (translate ID.MINUS)
(Call arraySize [srcN])
(Int 1)
srcStep = if Ty.isRangeType srcType
then Call rangeStep [srcN]
else Int 1

(srcStartN, srcStartT) <- translateSrc src A.start startVar srcStart
(srcStopN, srcStopT) <- translateSrc src A.stop stopVar srcStop
(srcStepN, srcStepT) <- translateSrc src A.step srcStepVar srcStep

(stepN, stepT) <- translate step
substituteVar name eltVar
(bodyN, bodyT) <- translate body
unsubstituteVar name

let stepDecl = Assign (Decl (int, stepVar))
(BinOp (translate ID.TIMES) stepN srcStepN)
stepAssert = Statement $ Call rangeAssertStep [stepVar]
indexDecl = Seq [AsExpr $ Decl (int, indexVar)
,If (BinOp (translate ID.GT)
(AsExpr stepVar) (Int 0))
(Assign indexVar srcStartN)
(Assign indexVar srcStopN)]
cond = BinOp (translate ID.AND)
(BinOp (translate ID.GTE) indexVar srcStartN)
(BinOp (translate ID.LTE) indexVar srcStopN)
eltDecl =
Assign (Decl (eltType, eltVar))
(if Ty.isRangeType srcType
then AsExpr indexVar
else AsExpr $ fromEncoreArgT eltType (Call arrayGet [srcN, indexVar]))
inc = Assign indexVar (BinOp (translate ID.PLUS) indexVar stepVar)
theBody = Seq [eltDecl, Statement bodyT, inc]
theLoop = While cond theBody

return (unit, Seq [srcT
,srcStartT
,srcStopT
,srcStepT
,stepT
,stepDecl
,stepAssert
,indexDecl
,theLoop])
where
translateSrc src selector var rhs
| A.isRangeLiteral src = translate (selector src)
| otherwise = return (var, Assign (Decl (int, var)) rhs)

translate ite@(A.IfThenElse { A.cond, A.thn, A.els }) =
do tmp <- Ctx.genNamedSym "ite"
(ncond, tcond) <- translate cond
Expand Down
Loading