Skip to content

Commit

Permalink
Merge pull request #216 from nbuilding/value-access
Browse files Browse the repository at this point in the history
Add the `[]` operator
  • Loading branch information
SheepTester authored Jul 9, 2021
2 parents 0933240 + 1d1ec91 commit 5b65fe6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 84 deletions.
42 changes: 41 additions & 1 deletion python/operation_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from type import NTypeVars, NGenericType
from native_types import n_list_type, list_generic, n_map_type
from native_types import n_list_type, list_generic, n_map_type, n_maybe_type

# Might move these into Scope one day because these might be scoped due to
# implementations of traits.
Expand All @@ -9,6 +9,10 @@
in_map_generic = NGenericType("k")
in_value_generic = NGenericType("v")

access_list_generic = NGenericType("t")
access_map_generic = NGenericType("k")
access_value_generic = NGenericType("v")

binary_operation_types = {
"OR": [("bool", "bool", "bool"), ("int", "int", "int")],
"AND": [("bool", "bool", "bool"), ("int", "int", "int")],
Expand Down Expand Up @@ -40,6 +44,42 @@
# Exponents are weird because negative powers result in non-integers.
# TODO: Make int ^ int an int; negative powers should result in 0.
"EXPONENT": [("int", "int", "float"), ("float", "float", "float")],
"VALUEACCESS": [
(
n_list_type.with_typevars([access_list_generic]),
"int",
n_maybe_type.with_typevars([access_list_generic]),
), (
n_map_type.with_typevars([
access_map_generic,
access_value_generic,
]),
access_map_generic,
n_maybe_type.with_typevars([access_value_generic]),
),
(
"str",
"int",
n_maybe_type.with_typevars(["char"]),
),
(
n_maybe_type.with_typevars([n_list_type.with_typevars([access_list_generic])]),
"int",
n_maybe_type.with_typevars([access_list_generic]),
), (
n_maybe_type.with_typevars([n_map_type.with_typevars([
access_map_generic,
access_value_generic,
])]),
access_map_generic,
n_maybe_type.with_typevars([access_value_generic]),
),
(
n_maybe_type.with_typevars(["str"]),
"int",
n_maybe_type.with_typevars(["char"]),
),
]
}
unary_operation_types = {
"SUBTRACT": [("int", "int"), ("float", "float")],
Expand Down
80 changes: 1 addition & 79 deletions python/run.n
Original file line number Diff line number Diff line change
@@ -1,79 +1 @@
import times
import FileIO
import request
import json
import websocket

let textToBytes = [text:str] -> list[int] {
let b = []
for (i in range(0, len(text), 1)) {
if let <yes c> = text |> charAt(i) {
var b = b |> append(charCode(c))
}
var b = b |> append(0)
}
return b
}

let thing = [] -> cmd[()] {
let imageData = FileIO.readBytes("C:\\Users\\Ashvin Ranjan\\Downloads\\IMG_1499.jpg")! |> default([1])
let image = ""
for (byte in imageData) {
var image = image + intCode(byte)
}
print(times.getTime()!)
let _ = FileIO.appendBytes("test.txt", [26, 26])!
print(FileIO.readBytes("test.txt")!)
print(request.delete("https://github.com", mapFrom([("", "")]))!)
let _ = request.createServer(3000,
[path:str requestType:str data:json.value] -> cmd[{ responseCode:int; data:list[int]; headers:map[str, str]; mimetype:str }] {
if (path == "" && requestType == "GET") {
return {
responseCode: 200
data: textToBytes("oops");
headers: mapFrom([
("Access-Control-Allow-Origin", "*"),
("Content-Type", "text/html"),
])
mimetype: "text/html"
}
}
return {
responseCode: 404
data: textToBytes("oops");
headers: mapFrom([
("Access-Control-Allow-Origin", "*"),
("Content-Type", "text/plain"),
])
mimetype: "text/plain"
}
}
)!
}


print(getType(thing))
print(getType(imp "./runner.n"))

let thing2 = [] -> cmd[()] {
print("opening the websocket!")
let _ = websocket.createServer(
{
onConnect: [user:websocket.user path:str] -> cmd[bool] {
print(user)
let _ = user.send("hello")!
return false
}
onMessage: [user:websocket.user message:str] -> cmd[bool] {
print(message)
let _ = user.send(message)!
// user.disconnect()
return false
}
onDisconnect: [user:websocket.user exitData:maybe[{ code: int; reason:str }]] -> cmd[bool] { return false }
},
3000
)!
}

let pub test = thing2()
print(["12", "1", "2"][1][1])
13 changes: 13 additions & 0 deletions python/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,19 @@ async def eval_expr(self, expr):
raise SyntaxError(
"Unexpected operation for unary_expression: %s" % operation
)
elif expr.data == "value_access":
left, _, right = expr.children
eval_left = await self.eval_expr(left)
if isinstance(eval_left, EnumValue) and eval_left == none:
return none
try:
if isinstance(eval_left, EnumValue):
eval_left = eval_left.values[0]
return yes(eval_left[await self.eval_expr(right)])
except Exception as err:
if isinstance(err, IndexError) or isinstance(err, KeyError):
return none
raise err
elif expr.data == "char":
val = expr.children[0]
if isinstance(val, lark.Tree):
Expand Down
11 changes: 9 additions & 2 deletions python/syntax.lark
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,18 @@ func_type: generic_declaration? (func_inner_type "->")+ not_func_type
// Exponentiation right to left associative
?exponent_expression: unary_expression [EXPONENT exponent_expression]
?unary_expression: await_expression
| NOT unary_expression
| NOT
| SUBTRACT unary_expression

?await_expression: record_access
| await_expression AWAIT
?record_access: value

?record_access: value_access
| await_expression "." CNAME

?value_access: value
| await_expression VALUEACCESS expression "]"

value: NUMBER
| BOOLEAN
| STRING
Expand Down Expand Up @@ -227,6 +233,7 @@ AWAIT: "!"
PUBLIC: "pub"
UNIT: "()"
EMPTY: "_"
VALUEACCESS: "["
STRING: /"(?:[^\r\n\\\"]|\\(?:[nrtv0fb\"\\]|u\{[0-9a-fA-F]+\}|\{.\}))*"/

%import common.SIGNED_NUMBER -> NUMBER
Expand Down
2 changes: 1 addition & 1 deletion tests/assertions/value-access.n
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let nestedMap = mapFrom([
),
])
assert type nestedMap["english"]["apple"] : maybe[str]
assert value nestedMap["english"]["apple"] == "orangen't"
assert value nestedMap["english"]["apple"] == yes("orangen't")

// Strings

Expand Down
2 changes: 1 addition & 1 deletion tests/syntax/value-access.n
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let a = -(
)!
)[(g |> h)]
)()
)([i | j)]
)([i | j])
)[([])]
)
let b = (
Expand Down

0 comments on commit 5b65fe6

Please sign in to comment.