Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
made the contract decorator optional
Browse files Browse the repository at this point in the history
  • Loading branch information
rjdbcm committed Nov 21, 2021
1 parent cdc3f1b commit 3907a84
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Aspidites/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
+ lit_rparen
).setParseAction(cvt_pragma)

pragmas = Combine(pragma + oneOf(" ".join(available_pragmas))).setParseAction(
pragmas = Combine(pragma + oneOf(" ".join(available_pragmas)) + Optional(lit_lparen + lit_rparen)).setParseAction(
cvt_pragma
)

Expand All @@ -298,7 +298,7 @@
+ identifier
+ def_args
+ _contract_expression
).setParseAction(lambda s, l, t: "\n@__contract()\n" + "".join(*t) + lit_colon)
).setParseAction(lambda s, l, t: "".join(*t) + lit_colon)
func_decl.setName("function declaration")
comment_line = (
Combine(Regex(r"`(?:[^`\n\r\\]|(?:``)|(?:\\(?:[^x]|x[0-9a-fA-F]+)))*") + "`")
Expand Down
2 changes: 1 addition & 1 deletion Aspidites/parser/reserved.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"cython.warn.unused_result",
"cython.warn.multiple_declarators",
]
available_pragmas = ["cfunc", "ccall", "nogil", "no_gc", "inline", "curried", "new_contract"]
available_pragmas = ["cfunc", "ccall", "contract", "no_gc", "inline", "curried", "new_contract"]
indent = " "
nl_indent = "\n" + indent
sep = ", "
Expand Down
2 changes: 2 additions & 0 deletions Aspidites/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def get_include():
from Aspidites._vendor.RestrictedPython.compile import compile_restricted as compile
from Aspidites.type_guard import safer_type as type
__safe_builtins['new_contract']=__new_contract
__safe_builtins['contract']=__contract
__safe_builtins['check']=check
__safe_builtins['type']=type
__safe_builtins['compile']=compile
Expand Down Expand Up @@ -239,6 +240,7 @@ def get_include():
coroutine: Generator
number: Any
new_contract = __new_contract
contract = __contract
$code
Expand Down
5 changes: 5 additions & 0 deletions Aspidites/tests/examples/examples.wom
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
`first-class functions`
#curried
#contract
(Add(x = 3 -> int; y = 3 -> int)) int
<*>x+y

Expand All @@ -9,12 +10,16 @@
<^>2
<^>3

#cython.boundscheck(False)
#cython.wraparound(False)
(sieve(n = /0 -> int; limit = /0 -> int; is_prime = [/0] -> *)) *
i<@>range(n**2, limit+1, n)
f = False
is_prime = is_prime[$]i,f
<*>is_prime

#cython.boundscheck(False)
#cython.wraparound(False)
(primes_upto(limit = 4 -> int)) list(int)
primes = [] -> list
upper = limit - 1 -> int
Expand Down
6 changes: 2 additions & 4 deletions Aspidites/tests/test_aspidites.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,15 @@ def test_compile_to_shared_object(inject_config):
assert match(4) == 7
assert idx == 1

with pt.raises(ContractNotRespected):
# noinspection PyTypeChecker
Add(x=6.5, y=12)
# Add(x=6.5, y=12)

assert [1, 2, 3] == [i for i in Yield123()]
assert __maybe(Add, 6.5, 12)() == __undefined()
assert x() == 6
assert y() == __undefined()
assert nullity == __undefined()
assert z == 9
assert Add(x=3, y=2) == 5
# assert __maybe(Add, x=3, y=2)() == 5
assert val() == __undefined()
assert div_by_zero == __undefined()

Expand Down
6 changes: 4 additions & 2 deletions Aspidites/woma/gcutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ def get_all(type_obj, include_subtypes=True):
"""Get a list containing all instances of a given type. This will
work for the vast majority of types out there.
>>> class Ratking(object): pass
>>> wiki, hak, sport = Ratking(), Ratking(), Ratking()
>>> RatKing = type('Ratking', {})
>>> foo = Ratking() -> *
>>> bar = Ratking() -> *
>>> baz = Ratking() -> *
>>> len(get_all(Ratking))
3
Expand Down
2 changes: 2 additions & 0 deletions Aspidites/woma/library.wom
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

`Here are some (not at all optimal) functional interfaces for atomic arithmetic ops`

#contract
(add(x = 0 -> number; y = 0 -> number)) number
<*> x + y

#contract
(sub(x = 0 -> number; y = 0 -> number)) number
<*> x - y

Expand Down
3 changes: 0 additions & 3 deletions Aspidites/woma/mathutils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# cython: language_level=3, annotation_typing=True, c_string_encoding=utf-8, boundscheck=False, wraparound=False, initializedcheck=False
"""This module provides useful math functions on top of Python's
built-in :mod:`math` module.
"""
from __future__ import division

from math import ceil as _ceil, floor as _floor
Expand Down

0 comments on commit 3907a84

Please sign in to comment.