Skip to content

Commit

Permalink
gypsum: change syntax for unit literal from {} to ()
Browse files Browse the repository at this point in the history
  • Loading branch information
jayconrod committed Jun 10, 2017
1 parent 75603c1 commit 0f90c59
Show file tree
Hide file tree
Showing 33 changed files with 303 additions and 203 deletions.
12 changes: 6 additions & 6 deletions codeswitch/test/BinaryCompatibility/foo.gy
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test-load-store-field =
o.y = "baz"
assert-eq(56i64, o.x)
assert-eq("baz", o.y)
{}
()

def test-load-store-inherited-field =
let o = DerivedFieldClass()
Expand All @@ -29,33 +29,33 @@ def test-load-store-inherited-field =
o.y = "baz"
assert-eq(56i64, o.x)
assert-eq("baz", o.y)
{}
()

def test-call-class-method =
let o = MethodClass()
assert-eq(34i64, o.f)
assert-eq("bar", o.g)
{}
()

def test-call-inherited-class-method =
let o = DerivedMethodClass()
assert-eq(34i64, o.f)
assert-eq("bar", o.g)
{}
()

def test-call-inherited-trait-method =
let o = DerivedClassFromTrait()
assert-eq(34i64, o.f)
assert-eq("bar", o.g)
{}
()

def main =
test-load-store-field
test-load-store-inherited-field
test-call-class-method
test-call-inherited-class-method
test-call-inherited-trait-method
{}
()

def assert-eq(expected: String, actual: String) =
if (expected != actual)
Expand Down
2 changes: 1 addition & 1 deletion codeswitch/test/arithmetic-evaluation-example.gy
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,5 @@ def main =
MulExpr(AddExpr(ConstExpr(Integer(1)), ConstExpr(Integer(2))), AddExpr(ConstExpr(Integer(3)), ConstExpr(Integer(0))))
let opt-expr = optimize(unopt-expr)
match (opt-expr)
case MulExpr(ConstExpr(a), ConstExpr(b)) if a.value == 3 && b.value == 3 => {}
case MulExpr(ConstExpr(a), ConstExpr(b)) if a.value == 3 && b.value == 3 => ()
case _ => throw Exception()
2 changes: 1 addition & 1 deletion codeswitch/test/bitset-example.gy
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import std.IllegalArgumentException

final class BitSet
private def this = {}
private def this = ()

static def create(max: i32) =
let bytes-needed = (max + 7i32) / 8i32
Expand Down
2 changes: 1 addition & 1 deletion codeswitch/test/box-example.gy
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Box[static T <: Object >: Nothing](private var value: T)

def set(new-value: T) =
value = new-value
{}
()

def contains-same-value[static S](other: Box[S]) = value === other.value

Expand Down
6 changes: 3 additions & 3 deletions codeswitch/test/dict.gy
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def test-empty-dict =
try
it.next
throw AssertionException()
catch (e: NoSuchElementException) {}
catch (e: NoSuchElementException) ()

assert-false(dict.get(I32.of(42i32)).is-defined)
assert-eq[I32](I32.of(12i32), dict.get-or-else(I32.of(42i32), I32.of(12i32)))
assert-false(dict.remove(I32.of(42i32)).is-defined)
{}
()

def test-insert-retrieve-remove =
let dict = Dict[I32, I32]()
Expand Down Expand Up @@ -82,4 +82,4 @@ def main =
test-empty-dict
test-insert-retrieve-remove
test-iter
{}
()
2 changes: 1 addition & 1 deletion codeswitch/test/finally-exception-in-catch.gy
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def main =
try
inner-fn(outer-exn)
catch
case exn if exn === outer-exn && finally-executed => {}
case exn if exn === outer-exn && finally-executed => ()

def inner-fn(outer-exn: Exception): unit =
let inner-exn = Exception()
Expand Down
2 changes: 1 addition & 1 deletion codeswitch/test/large-alloc.gy
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test-bad-alloc(length: i32) =
new(length) Foo
throw Exception()
catch (e: OutOfMemoryException)
{}
()

def main =
test-bad-alloc(0x20000000i32)
Expand Down
8 changes: 4 additions & 4 deletions codeswitch/test/load-uninitialized.gy
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ def load-uninitialized-field =
foo.bar
throw Exception()
catch (e: UninitializedException)
{}
()

def load-uninitialized-element =
let foo = new(1i32) Foo
try
foo.get(0i32)
throw Exception()
catch (e: UninitializedException)
{}
()

def load-uninitialized-global =
try
global
throw Exception()
catch (e: UninitializedException)
{}
()

def load-uninitialized-capture =
var x: String
Expand All @@ -42,7 +42,7 @@ def load-uninitialized-capture =
f
throw Exception()
catch (e: UninitializedException)
{}
()

def main =
load-uninitialized-field
Expand Down
2 changes: 1 addition & 1 deletion codeswitch/test/match-static-dynamic.gy
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class FullBox[static T](value: T) <: Box[T]
def main =
let box = FullBox[String]("foo")
match (box)
case FullBox[String](s) if s == "foo" => {}
case FullBox[String](s) if s == "foo" => ()
case _ => throw Exception()
2 changes: 1 addition & 1 deletion codeswitch/test/primitives-to-string.gy
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def check-eq(expected: String, actual: String) =
throw Exception()

def main =
check-eq("unit", {}.to-string)
check-eq("unit", ().to-string)

// check-eq("true", true.to-string)
// check-eq("false", false.to-string)
Expand Down
2 changes: 1 addition & 1 deletion codeswitch/test/result.gy
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ def main =
catch (_: Exception) true
assert-true(e)
match (err)
case Err[String](s) if s === message => {}
case Err[String](s) if s === message => ()
case _ => throw AssertionException()
2 changes: 1 addition & 1 deletion codeswitch/test/return-nested-finally.gy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def f =
inner-finally-executed = true
catch (_)
catch-executed = true
{}
()
finally
outer-finally-executed = true

Expand Down
4 changes: 2 additions & 2 deletions codeswitch/test/set.gy
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test-empty-set =
try
it.next
throw AssertionException()
catch (e: NoSuchElementException) {}
catch (e: NoSuchElementException) ()

assert-false(set.contains(I32.of(1i32)))
assert-false(set.remove(I32.of(1i32)))
Expand Down Expand Up @@ -80,4 +80,4 @@ def main =
test-empty-set
test-insert-remove
test-iter
{}
()
4 changes: 2 additions & 2 deletions codeswitch/test/std-io-length.gy
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def read-all(reader: Reader): i64 =
let length = reader.read(buffer, 0i32, buffer.length)
if (length == 0i32)
done = true
{}
()
else
total-length += length.to-i64
{}
()
total-length

def main =
Expand Down
4 changes: 2 additions & 2 deletions codeswitch/test/string-access.gy
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def main =
s.get(-1i32)
throw Exception()
catch (e: ArrayIndexOutOfBoundsException)
{}
()

try
s.get(5i32)
throw Exception()
catch (e: ArrayIndexOutOfBoundsException)
{}
()
2 changes: 1 addition & 1 deletion codeswitch/test/uninitialized-global.gy
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def main =
try
g
throw Exception()
catch (_: UninitializedException) {}
catch (_: UninitializedException) ()
2 changes: 1 addition & 1 deletion examples/bitset.gy
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import std.IllegalArgumentException
final class BitSet
// We declare a private constructor so clients are forced to use the `create` method to
// build new instances. `BitSet` has no normal fields, so this method does nothing.
private def this = {}
private def this = ()

// This method creates new `BitSet` objects. We give it a maximum integer that can be stored
// in the set, and it calculates the number of bytes needed, then allocates a new `BitSet`.
Expand Down
2 changes: 1 addition & 1 deletion examples/box.gy
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Box[static T <: Object >: Nothing](private var value: T)

def set(new-value: T) =
value = new-value
{}
()

// Of course methods may have their own type parameters.
def contains-same-value[static S](other: Box[S]) = value === other.value
Expand Down
2 changes: 1 addition & 1 deletion gypsum/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def visitGroupExpression(self, node):
self._write(")")

def visitUnitLiteral(self, node):
self._write("{}")
self._write("()")

def visitIntegerLiteral(self, node):
self._write(node.text)
Expand Down
65 changes: 41 additions & 24 deletions gypsum/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
INDENT,
INTEGER,
LAMBDA,
LBRACE,
LBRACK,
LET,
LPAREN,
Expand Down Expand Up @@ -114,7 +113,6 @@ def __init__(self, fileName, tokens):
EXPR_START_TOKENS = (
LBRACK,
LPAREN,
LBRACE,
IF,
ELSE,
WHILE,
Expand Down Expand Up @@ -248,8 +246,12 @@ def varDefn(self, l, ats):
pat = self.pattern()
if self._peekTag() is EQ:
self._next()
# Use maybeTupleExpr instead of expr to avoid assignments.
e = self.maybeTupleExpr()
# TODO: this hack is here to avoid assignment. Remove when assignment is
# no longer an expression.
if self._peekTag() is NEWLINE:
e = self.blockExpr()
else:
e = self.maybeTupleExpr()
else:
e = None
d = ast.VariableDefinition(ats, var, pat, e, None, self._location(l))
Expand Down Expand Up @@ -472,12 +474,18 @@ def literalPattern(self):
return p

def groupPattern(self):
l = self._nextTag(LPAREN)
p = self.pattern()
self._nextTag(RPAREN)
g = ast.GroupPattern(p, None, self._location(l.location))
self._tailComment(g)
return g
l = self._nextTag(LPAREN).location
if self._peekTag() is RPAREN:
self._next()
loc = self._location(l)
lit = ast.UnitLiteral(loc)
p = ast.LiteralPattern(lit, None, loc)
else:
i = self.pattern()
self._nextTag(RPAREN)
p = ast.GroupPattern(i, None, self._location(l))
self._tailComment(p)
return p

# Types
def ty(self):
Expand Down Expand Up @@ -585,7 +593,10 @@ def typeFlags(self):

# Expressions
def expr(self):
return self.maybeAssignExpr()
if self._peekTag() is NEWLINE:
return self.blockExpr()
else:
return self.maybeAssignExpr()

def maybeAssignExpr(self):
es = self._parseRepSep(self.maybeTupleExpr, EQ)
Expand Down Expand Up @@ -653,7 +664,9 @@ def _associativity(self, op):

def maybeCallExpr(self):
e = self.receiverExpr()
tag = self._peek().tag
if self._peekTag(-1) is OUTDENT:
return e
tag = self._peekTag()
while tag in (DOT, LBRACK, LPAREN):
if tag is DOT:
self._next()
Expand Down Expand Up @@ -681,7 +694,7 @@ def receiverExpr(self):
tag = self._peek().tag
if tag is OPERATOR:
e = self.unaryExpr()
elif tag in (INTEGER, FLOAT, STRING, TRUE, FALSE, NULL, LBRACE):
elif tag in (INTEGER, FLOAT, STRING, TRUE, FALSE, NULL):
e = self.literalExpr()
elif tag is SYMBOL:
e = self.varExpr()
Expand All @@ -707,8 +720,6 @@ def receiverExpr(self):
e = self.throwExpr()
elif tag is TRY:
e = self.tryExpr()
elif tag is NEWLINE:
e = self.blockExpr()
elif tag is LAMBDA:
e = self.lambdaExpr()
elif tag is RETURN:
Expand Down Expand Up @@ -751,12 +762,18 @@ def superExpr(self):
return e

def groupExpr(self):
l = self._nextTag(LPAREN)
e = self.expr()
r = self._nextTag(RPAREN)
g = ast.GroupExpression(e, None, l.location.combine(r.location))
self._tailComment(g)
return g
l = self._nextTag(LPAREN).location
if self._peekTag() is RPAREN:
self._next()
loc = self._location(l)
lit = ast.UnitLiteral(loc)
e = ast.LiteralExpression(lit, None, loc)
else:
i = self.expr()
self._nextTag(RPAREN)
e = ast.GroupExpression(i, None, self._location(l))
self._tailComment(e)
return e

def newArrayExpr(self):
l = self._nextTag(NEW)
Expand Down Expand Up @@ -943,7 +960,7 @@ def returnExpr(self):
# Literals
def literal(self):
tok = self._peek()
if tok.tag is LBRACE:
if tok.tag is LPAREN:
return self.unitLiteral()
elif tok.tag is INTEGER:
return self.intLiteral()
Expand All @@ -963,8 +980,8 @@ def literal(self):
return lit

def unitLiteral(self):
l = self._nextTag(LBRACE).location
self._nextTag(RBRACE)
l = self._nextTag(LPAREN).location
self._nextTag(RPAREN)
return ast.UnitLiteral(self._location(l))

def intLiteral(self):
Expand Down
Loading

0 comments on commit 0f90c59

Please sign in to comment.