Skip to content

Commit

Permalink
fixing stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
tmoux committed Jul 15, 2020
1 parent a4f6d81 commit 959f4d9
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 126 deletions.
47 changes: 25 additions & 22 deletions selfhost/binding.wyv
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@ def rawToType(s:raw.Type, ctx:types.Context):types.Type
if (b.str == "Unit")
types.UnitType()
else
val pred = ((d:DeclType) =>
val z = match d:
tt:types.TypeType =>
if (tt.name.name == b.str) { option.Some[types.TypeType](tt) } else { option.None[types.TypeType]() }
default => option.None[types.TypeType]()
z
)
val pred = typesUtil.findTypePredicateFromStr(b.str)
val L = typesUtil.findInDeclList[types.TypeType](ctx.bindings, pred).name
types.NominalType(L)
path:raw.Path =>
val p = bindExpr(path.p,ctx)
val t = types.Binding(path.t,ctx.counter) //maybe should just be a String?
types.PathType(p,t)
types.PathType(p,path.t)
val refines = s.refines.map[types.DeclType](e => bindDeclTypes(e,ctx))
types.Type(base,refines)

Expand All @@ -39,12 +32,12 @@ def bindDeclTypes(e:raw.Exp, context:types.Context):DeclType = match e:
val b = types.Binding(d.name, context.counter)

var newCtx:types.Context = context
def mapParams(args:List[raw.Arg]):List[types.Arg] = match args:
def mapParams(args:List[raw.Arg]):List[types.ValType] = match args:
c:llist.Cons =>
val arg = types.Arg(types.Binding(c.value.arg,newCtx.counter),rawToType(c.value.argTyp,newCtx))
newCtx = newCtx.extend(types.ValType(arg.arg,arg.argTyp))
llist.Cons[types.Arg](arg,mapParams(c.next))
n:llist.Nil => llist.Nil[types.Arg]()
val arg = types.ValType(types.Binding(c.value.arg,newCtx.counter),rawToType(c.value.argTyp,newCtx))
newCtx = newCtx.extend(arg)
llist.Cons[types.ValType](arg,mapParams(c.next))
n:llist.Nil => llist.Nil[types.ValType]()
val arglist = mapParams(d.args)
types.DefType(b, arglist, rawToType(d.retTyp,newCtx))

Expand Down Expand Up @@ -74,7 +67,17 @@ def makeSeq(e:raw.Exp):raw.Seq = match e: //ensures that the whole expression is
default => raw.Seq(llist.Singleton[raw.Exp](e))

def bind(e:raw.Exp, parse: String -> raw.Exp):types.Statement
bindStatement(makeSeq(e), types.emptyContext(parse))
bindTopLevel(e, types.emptyContext(parse))

def bindTopLevel(e:raw.Exp, context:types.Context):types.Statement
//add Unit type decl
val nameb = types.Binding("Unit",context.counter)
val zb = types.Binding("z",context.counter)
val unitTypeDecl = types.TypeDecl(nameb,zb,llist.Nil[DeclType]())
val unitDeclType = types.TypeType(nameb,zb,llist.Nil[DeclType]())

val program = bindStatement(makeSeq(e),context.extend(unitDeclType))
types.DeclStatement(unitTypeDecl,program)

def bindStatement(e:raw.Seq, context:types.Context):types.Statement = match e.exps:
c:llist.Cons => match c.next:
Expand Down Expand Up @@ -114,12 +117,12 @@ def bindDecl(e:raw.Exp, context:types.Context):types.Decl = match e:
val b = types.Binding(d.name, context.counter)

var newCtx:types.Context = context
def mapParams(args:List[raw.Arg]):List[types.Arg] = match args:
def mapParams(args:List[raw.Arg]):List[types.ValType] = match args:
c:llist.Cons =>
val arg = types.Arg(types.Binding(c.value.arg,newCtx.counter),rawToType(c.value.argTyp,newCtx))
newCtx = newCtx.extend(types.ValType(arg.arg,arg.argTyp))
llist.Cons[types.Arg](arg,mapParams(c.next))
n:llist.Nil => llist.Nil[types.Arg]()
val arg = types.ValType(types.Binding(c.value.arg,newCtx.counter),rawToType(c.value.argTyp,newCtx))
newCtx = newCtx.extend(arg)
llist.Cons[types.ValType](arg,mapParams(c.next))
n:llist.Nil => llist.Nil[types.ValType]()
val arglist = mapParams(d.args)

val methodDecl = types.DefType(b, arglist, rawToType(d.retTyp,newCtx))
Expand Down Expand Up @@ -185,8 +188,8 @@ def bindExpr(e:raw.Exp, context:types.Context):types.Exp = match e:
val expType = typecheck.typecheckExpr(boundExp, newCtx.bindings)

val applyb = types.Binding("apply", context.counter)
val decl = types.DefType(applyb, llist.Singleton[types.Arg](types.Arg(argb,argDecl.typ)), expType)
val defDeclaration:types.Decl = types.Def(applyb, llist.Singleton[types.Arg](types.Arg(argb,argDecl.typ)), expType, types.ExprStatement(boundExp))
val decl = types.DefType(applyb, llist.Singleton[types.ValType](types.ValType(argb,argDecl.typ)), expType)
val defDeclaration:types.Decl = types.Def(applyb, llist.Singleton[types.ValType](types.ValType(argb,argDecl.typ)), expType, types.ExprStatement(boundExp))

val thisb = types.Binding("_this", context.counter)
val thisType = types.makeRefines(llist.Singleton[DeclType](decl))
Expand Down
15 changes: 15 additions & 0 deletions selfhost/failtests/badsubtype.wyv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type Int:z:
def +(i:Int):Int
def -(i:Int):Int

type A:z:
type T >= Int
val a:z.T

type B:z:
type T <= Int
val a:z.T

subtype B extends A

0
19 changes: 19 additions & 0 deletions selfhost/failtests/badsubtype2.wyv
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type Int:z:
def +(i:Int):Int
def -(i:Int):Int

type A:z:
type T >= Int
val a:z.T

type B:z:
type T <= Int
val a:z.T

type C:z:
val a:Int

subtype Unit {val a:Int} extends C
subtype Unit {val a:Int} extends A

0
36 changes: 19 additions & 17 deletions selfhost/main.wyv
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,24 @@ def parse(s:String):raw.Exp

stdout.print("Binding...\n")
val boundExp:types.Statement = binding.bind(parser.results.get(0), s => parse(s))
//val boundExp : exception.Answer[types.Statement,exception.Exception] = exception.try[types.Statement](() => binding.bind(parser.results.get(0), s => parse(s)))
//match boundExp:
// s1: exception.Success =>
// stdout.print("binding success\n")
// val tc:exception.Answer[types.Type,exception.Exception] = exception.try[types.Type](() => typecheck.typecheck(s1.value))
// match tc:
// s1: exception.Success =>
// stdout.print("type: " + typesUtil.typeToString(s1.value) + "\n")
// f1: exception.Failure =>
// match f1.exception:
// exn: error.ErrorReportingException => stdout.print(error.asString(exn)+"\n")
// default => stdout.print("unexpected exception thrown\n")
// f1: exception.Failure =>
// match f1.exception:
// exn: error.ErrorReportingException => stdout.print(error.asString(exn)+"\n")
// default => stdout.print("unexpected exception thrown\n")

val boundExp : exception.Answer[types.Statement,exception.Exception] = exception.try[types.Statement](() => binding.bind(parser.results.get(0), s => parse(s)))
match boundExp:
s1: exception.Success =>
stdout.print("binding success\n")
val tc:exception.Answer[types.Type,exception.Exception] = exception.try[types.Type](() => typecheck.typecheck(s1.value))
match tc:
s1: exception.Success =>
stdout.print("type: " + typesUtil.typeToString(s1.value) + "\n")
f1: exception.Failure =>
match f1.exception:
exn: error.ErrorReportingException => stdout.print(error.asString(exn)+"\n")
default => stdout.print("unexpected exception thrown\n")
f1: exception.Failure =>
match f1.exception:
exn: error.ErrorReportingException => stdout.print(error.asString(exn)+"\n")
default => stdout.print("unexpected exception thrown\n")

/*
val tc = typecheck.typecheck(boundExp)
stdout.print("type: " + typesUtil.typeToString(tc) + "\n")

Expand All @@ -93,3 +94,4 @@ stdout.print(outputFileName)
stdout.print("...\n")

toBytecode.writeExpToFile(ooExp, outputFileName)
*/
6 changes: 3 additions & 3 deletions selfhost/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ wyby main.wyv
node ../backend/boot.js main.wyb > main.js
node main.js $1

echo FINISHED PRODUCING WYB FILE FOR $1 NOW RUNNING IT
node ../backend/boot.js `echo $1 | sed 's/\.wyv/\.wyb/'` > `echo $1 | sed 's/\.wyv/\.js/'`
node `echo $1 | sed 's/\.wyv/\.js/'`
#echo FINISHED PRODUCING WYB FILE FOR $1 NOW RUNNING IT
#node ../backend/boot.js `echo $1 | sed 's/\.wyv/\.wyb/'` > `echo $1 | sed 's/\.wyv/\.js/'`
#node `echo $1 | sed 's/\.wyv/\.js/'`
26 changes: 26 additions & 0 deletions selfhost/tests/cloneable.wyv
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
type Int:z:
def +(i:Int):Int
def -(i:Int):Int

type Cloneable:z:
type T <= Unit
def clone(u:Unit):z.T

type String:z:
type T <= String
def clone(u:Unit):z.T
subtype String extends Cloneable

type A:z:
def makeClone(arg:Cloneable):arg.T
val a = new this:A:
def makeClone(arg:Cloneable):arg.T:
arg.clone(())

val s = new this:String:
type T <= String
def clone(u:Unit):this.T:
a.makeClone(this)


a.makeClone(s)
4 changes: 2 additions & 2 deletions selfhost/tests/lambdaTest.wyv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type Int:
type Int:z:
def +(i:Int):Int
def -(i:Int):Int

val succ = x:Int => x + 1
val fst = x:Int => y:Int => x
fst 7
fst
5 changes: 3 additions & 2 deletions selfhost/tests/monoid.wyv
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Monoid:z:
type T <= Unit
val id:z.T
def op(a:z.T, b:z.T):z.T
subtype Monoid extends Semi

val sum = new this:Monoid {type T = Int}:
type T = Int
Expand All @@ -18,10 +19,10 @@ val sum = new this:Monoid {type T = Int}:
a + b

type Util:z:
def apply3(s:Monoid, a:s.T, b:s.T, c:s.T):s.T
def apply3(s:Semi, a:s.T, b:s.T, c:s.T):s.T

val util = new this:Util:
def apply3(s:Monoid, a:s.T, b:s.T, c:s.T):s.T:
def apply3(s:Semi, a:s.T, b:s.T, c:s.T):s.T:
s.op(a,s.op(b,c))

val ans = util.apply3(sum,1,2,3)
Expand Down
7 changes: 4 additions & 3 deletions selfhost/tests/typemember.wyv
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ type Int:z:

type Equatable:z:
type T <= Unit
def equals(x:z.T):Int
def equals(x:z.T):z.T

type Fruit:z:
type T <= Unit
type S <= Unit
val item:z.T
def equals(x:z.T):Int
def equals(x:z.T):z.T
subtype Fruit extends Equatable

val f1 = new this:Fruit {type T = Int}:
type T = Int
type S <= Unit
val item:this.T = 3
def equals(x:this.T):Int:
def equals(x:this.T):this.T:
this.item + x

f1.equals(3)
2 changes: 1 addition & 1 deletion selfhost/toBytecode.wyv
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def toBytecodeStmt(e:types.Statement):List[b.SeqStmt] = match e:

def toBytecodeDecl(e:types.Decl):b.Decl = match e:
v:types.Val => b.ValDecl(v.binding.name, toBytecodeExpr(v.exp))
d:types.Def => b.MethodDecl(d.binding.name, d.args.map[String](e => e.arg.name), b.SeqExpr(toBytecodeStmt(d.body)))
d:types.Def => b.MethodDecl(d.binding.name, d.args.map[String](e => e.name.name), b.SeqExpr(toBytecodeStmt(d.body)))
t:types.TypeDecl => b.ValDecl("_", toBytecodeExpr(types.UnitVal()))
s:types.SubtypeDecl => b.ValDecl("_", toBytecodeExpr(types.UnitVal()))
m:types.TypeEq => b.ValDecl("_", toBytecodeExpr(types.UnitVal()))
Expand Down
Loading

0 comments on commit 959f4d9

Please sign in to comment.