Skip to content

Commit

Permalink
Use square brackets for parameterized types
Browse files Browse the repository at this point in the history
  • Loading branch information
tjammer committed Aug 26, 2024
1 parent 6cb5801 commit 19d7d77
Show file tree
Hide file tree
Showing 51 changed files with 285 additions and 271 deletions.
5 changes: 3 additions & 2 deletions lib/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
%left Plus_op
%left Mult_op
%left Dot Ampersand Exclamation
%left Lbrack
%left Lpar
%left Path Hashtag_brack

Expand Down Expand Up @@ -192,7 +193,7 @@ record_item_decl:

decl_typename:
| name = Ident { { name; poly_param = [] } }
| name = Ident; Lpar; poly_param = separated_nonempty_list(Comma, poly_id); Rpar { { name; poly_param } }
| name = Ident; Lbrack; poly_param = separated_nonempty_list(Comma, poly_id); Rbrack { { name; poly_param } }

%inline module_decl:
| name = ident { let loc, name = name in loc, name, None }
Expand Down Expand Up @@ -483,7 +484,7 @@ type_spec:
| id = Sized_ident { Ty_id id }
| id = Unknown_sized_ident { Ty_id id }
| path = type_path { Ty_use_id ($loc, path) }
| head = type_spec; Lpar; tail = separated_nonempty_list(Comma, type_spec); Rpar
| head = type_spec; Lbrack; tail = separated_nonempty_list(Comma, type_spec); Rbrack
{ Ty_applied (head :: tail) }
| Lpar; Rpar; Right_arrow; ret = type_spec; %prec Type_application
{ Ty_func ([Ty_id "unit", Dnorm; ret, Dnorm]) }
Expand Down
40 changes: 24 additions & 16 deletions lib/syntax_errors.messages
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ prog: Type With

prog: Type Ident With

<YOUR SYNTAX ERROR MESSAGE HERE>
Expecting square brackets for parameterized type

prog: Type Ident Lpar With
prog: Type Ident Lbrack With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: External Ident Colon Quote With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Type Ident Lpar Quote Ident With
prog: Type Ident Lbrack Quote Ident With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Type Ident Lpar Quote Ident Comma With
prog: Type Ident Lbrack Quote Ident Comma With

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand Down Expand Up @@ -66,19 +66,23 @@ prog: External Ident Colon Lpar Rpar Right_arrow Ident With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: External Ident Colon Ident Lpar With
prog: External Ident Colon Ident Lbrack With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: External Ident Colon Ident Lpar Ident Rbrac
prog: External Ident Colon Ident Lbrack Ident Rbrac

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: External Ident Colon Ident Lpar Ident Comma With
prog: External Ident Colon Ident Lbrack Ident Comma With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: External Ident Colon Lpar Ident Rbrac
prog: External Ident Colon Ident Lbrack Ident Rpar

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: External Ident Colon Lpar Ident Rbrack

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand Down Expand Up @@ -114,7 +118,7 @@ prog: External Ident Colon Lpar Ident Ampersand Comma With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: External Ident Colon Lpar Ident Ampersand Comma Ident Rbrac
prog: External Ident Colon Lpar Ident Ampersand Comma Ident Rbrack

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand All @@ -126,7 +130,7 @@ prog: External Ident Colon Lpar Ident Comma With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: External Ident Colon Lpar Ident Comma Ident Rbrac
prog: External Ident Colon Lpar Ident Comma Ident Rbrack

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand All @@ -150,7 +154,7 @@ prog: External Ident Colon Lpar Ident Comma Ident Comma With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: External Ident Colon Lpar Ident Comma Ident Comma Ident Rbrac
prog: External Ident Colon Lpar Ident Comma Ident Comma Ident Rbrack

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand Down Expand Up @@ -188,7 +192,7 @@ prog: Type Ident Equal Lbrac Ident Colon With

prog: Type Ident Equal Lbrac Ident Colon Ident Rpar

<YOUR SYNTAX ERROR MESSAGE HERE>
Expecting square brackets for parameterized type

prog: Type Ident Equal Lbrac Ident Colon Ident Comma With

Expand All @@ -210,14 +214,18 @@ prog: Type Ident Equal Ctor Lpar Int With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Type Ident Equal Ctor Lpar Ident Rbrac
prog: Type Ident Equal Ctor Lpar Ident Rbrack

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Type Ident Equal Ctor Lpar Ident Comma With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Type Ident Equal Ctor Lpar Ident Comma Ident Rbrack

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Type Ident Equal Ctor Lpar Ident Rpar With

<YOUR SYNTAX ERROR MESSAGE HERE>
Expand All @@ -228,7 +236,7 @@ prog: Type Ident Equal Ctor Hbar With

prog: Type Ident Equal Ident Rpar

<YOUR SYNTAX ERROR MESSAGE HERE>
Expecting square brackets for parameterized type

prog: Signature With

Expand Down Expand Up @@ -274,7 +282,7 @@ prog: Signature Colon Begin Type With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Signature Colon Begin Type Ident Lpar Quote Ident Rpar With
prog: Signature Colon Begin Type Ident Lbrack Quote Ident Rbrack With

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand Down Expand Up @@ -390,7 +398,7 @@ prog: Fun Lpar Ident Colon With

<YOUR SYNTAX ERROR MESSAGE HERE>

prog: Fun Lpar Ident Colon Ident Rbrac
prog: Fun Lpar Ident Colon Ident Rbrack

<YOUR SYNTAX ERROR MESSAGE HERE>

Expand Down
4 changes: 2 additions & 2 deletions lib/typing/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let string_of_type_raw get_name typ mname =
| [] -> Path.(rm_name mname name |> show)
| l ->
let arg = String.concat ", " (List.map string_of_type l) in
Printf.sprintf "%s(%s)" Path.(rm_name mname name |> show) arg
Printf.sprintf "%s[%s]" Path.(rm_name mname name |> show) arg
end
| Qvar str | Tvar { contents = Unbound (str, _) } -> get_name str
| Tfixed_array ({ contents = sz }, t) ->
Expand All @@ -111,7 +111,7 @@ let string_of_type_raw get_name typ mname =
| Known i -> string_of_int i
| Linked iv -> size !iv
in
sprintf "array#%s(%s)" (size sz) (string_of_type t)
sprintf "array#%s[%s]" (size sz) (string_of_type t)
in

string_of_type typ
Expand Down
5 changes: 5 additions & 0 deletions schmu-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
(rx symbol-start (group (seq (any lower ?_) (* (any word ?_))))
(* space) "("))

(defconst schmu-type-param-pattern
(rx symbol-start (group (seq (any lower ?_) (* (any word ?_))))
(* space) "["))

(defconst schmu-module-pattern
(rx symbol-start (or "module" "module type" "functor" "use") (1+ space)
(group (seq (any word ?_) (* (any word ?_))))))
Expand Down Expand Up @@ -72,6 +76,7 @@
(,schmu-types-regexp . font-lock-type-face)
(,schmu-variable-pattern 1 font-lock-variable-name-face)
(, schmu-call-pattern 1 font-lock-function-name-face)
(, schmu-type-param-pattern 1 font-lock-type-face)
(,schmu-upcase-pattern 1 font-lock-type-face)
(,schmu-path-pattern 1 font-lock-type-face))
"Schmu keywords highlighting.")
Expand Down
32 changes: 16 additions & 16 deletions std/hashtbl.smu
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ module type key:

module type sig:
type key
type t('value)

val create : (int) -> t('value)
val length : (t('value)) -> int
val insert : (t('value)&, key!, 'value!) -> unit
val remove : (t('value)&, key) -> unit
val find : (t('value), key) -> option/t('value)
val find_map : (t('value), key, ('value) -> 'a) -> option/t('a)
val find_mut : (t('value)&, key, ('value&) -> unit) -> bool
val mem : (t('value), key) -> bool
val fold : (t('value), 'b!, ('b!, key, 'value) -> 'c) -> 'c
val clear : (t('value)&) -> unit
val to_array : (t('value)) -> array((key, 'value))
type t['value]

val create : (int) -> t['value]
val length : (t['value]) -> int
val insert : (t['value]&, key!, 'value!) -> unit
val remove : (t['value]&, key) -> unit
val find : (t['value], key) -> option/t['value]
val find_map : (t['value], key, ('value) -> 'a) -> option/t['a]
val find_mut : (t['value]&, key, ('value&) -> unit) -> bool
val mem : (t['value], key) -> bool
val fold : (t['value], 'b!, ('b!, key, 'value) -> 'c) -> 'c
val clear : (t['value]&) -> unit
val to_array : (t['value]) -> array[(key, 'value)]

functor make : sig(m : key):
use option
use prelude

type key = m/t
type item('a) = {key : m/t, value& : 'a, pathlen& : int}
type slot('a) = option/t(item('a))
type t('a) = {data& : array(slot('a)), nitems& : int}
type item['a] = {key : m/t, value& : 'a, pathlen& : int}
type slot['a] = option/t[item['a]]
type t['a] = {data& : array[slot['a]], nitems& : int}

fun rec power_2_above(x, n):
if x >= n: x else:
Expand Down
20 changes: 10 additions & 10 deletions std/in_channel.smu
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ import string
signature:
type t

val open : (string/t) -> option/t(t)
val open : (string/t) -> option/t[t]
val close : (t) -> unit

-- high level interface
val with_open : (string/t, (t&) -> 'a) -> option/t('a)
val with_open : (string/t, (t&) -> 'a) -> option/t['a]
val readall : (t&) -> string/t
val lines : (t&, (string/t) -> unit) -> unit

-- low level interface
type buf = array(u8)
val readbuf : (t&, buf&) -> option/t(int)
val readn : (t&, buf&, int) -> option/t(int)
val readline : (t&, buf&) -> option/t(int)
val readrem : (t&, buf&) -> option/t(int)
type buf = array[u8]
val readbuf : (t&, buf&) -> option/t[int]
val readn : (t&, buf&, int) -> option/t[int]
val readline : (t&, buf&) -> option/t[int]
val readrem : (t&, buf&) -> option/t[int]

type file
val of_file : (file) -> t

type file = raw_ptr(u8)
type file = raw_ptr[u8]

type t = {buf& : array#4096(u8), head& : int, unseen& : int, file : file}
type t = {buf& : array#4096[u8], head& : int, unseen& : int, file : file}

type cstr = raw_ptr(u8)
type cstr = raw_ptr[u8]
external fread : (cstr, int, int, file) -> int
external fopen : (cstr, cstr) -> file
external fclose : (cstr) -> int
Expand Down
2 changes: 1 addition & 1 deletion std/option.smu
Original file line number Diff line number Diff line change
@@ -1 +1 @@
type t('a) = #some('a) | #none
type t['a] = #some('a) | #none
2 changes: 1 addition & 1 deletion std/std.smu
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let println = string/println
let print = string/print
let cstr = string/data

type option('a) = option/t('a)
type option['a] = option/t['a]

-- prelude re-exports
let char_equal = prelude/char_equal
Expand Down
26 changes: 13 additions & 13 deletions std/string.smu
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import option
import prelude

signature:
type cstr = raw_ptr(u8)
type cstr = raw_ptr[u8]
type t

val len : (t) -> int
Expand All @@ -12,18 +12,18 @@ signature:
val hash : (t) -> int
val equal: (t, t) -> bool
val append : (t&, t) -> unit
val concat : (t, array(t)) -> t
val boyer_moore_horspool : (t, t) -> option/t(int)
val concat : (t, array[t]) -> t
val boyer_moore_horspool : (t, t) -> option/t[int]

val modify_buf : (t&, (array(u8)&) -> unit) -> unit
val of_array : (array(u8)!) -> t
val to_array : (t!) -> array(u8)
val of_fixed_array : (array#?(u8)) -> t
val modify_buf : (t&, (array[u8]&) -> unit) -> unit
val of_array : (array[u8]!) -> t
val to_array : (t!) -> array[u8]
val of_fixed_array : (array#?[u8]) -> t

val println : (t) -> unit
val print : (t) -> unit

type t = array(u8)
type t = array[u8]

-- TODO use aliases
fun len(str : t): array/length(str)
Expand All @@ -45,7 +45,7 @@ fun hash(str):
array/fold(str, !5381, fun (hash!, c):
(hash * 33) + u8_to_int(c))

external memcmp : (raw_ptr(u8), raw_ptr(u8), int) -> i32
external memcmp : (raw_ptr[u8], raw_ptr[u8], int) -> i32

fun equal(l, r):
if len(l) == len(r):
Expand Down Expand Up @@ -103,17 +103,17 @@ fun modify_buf(str& : t, f):
-- return type needs to be inferred as unit
()

fun of_array(arr! : array(u8)): arr
fun of_array(arr! : array[u8]): arr
fun to_array(str! : t): str

external memcpy : (raw_ptr(u8), raw_ptr(u8), int) -> unit
external memcpy : (raw_ptr[u8], raw_ptr[u8], int) -> unit

fun of_fixed_array(arr : array#?(u8)):
fun of_fixed_array(arr : array#?[u8]):
let ret = array/create(__fixed_array_length(arr))
memcpy(array/data(ret), __fixed_array_data(arr), __fixed_array_length(arr))
ret

type file = raw_ptr(u8)
type file = raw_ptr[u8]
external stdout : file
external fwrite : (cstr, int, int, file) -> unit

Expand Down
16 changes: 8 additions & 8 deletions std/unsafe.smu
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
signature:
val nullptr : () -> raw_ptr(u8)
val nullptr : () -> raw_ptr[u8]

-- functions on raw pointers
val get : (raw_ptr('a), int) -> 'a
val +> : (raw_ptr('a), int) -> 'a
val at : (raw_ptr('a), int) -> raw_ptr('a)
val reinterpret : (raw_ptr('a)) -> raw_ptr('b)
val addr : ('a&) -> raw_ptr('a)
val funptr : ('a) -> raw_ptr(unit)
val clsptr : ('a) -> raw_ptr(unit)
val get : (raw_ptr['a], int) -> 'a
val +> : (raw_ptr['a], int) -> 'a
val at : (raw_ptr['a], int) -> raw_ptr['a]
val reinterpret : (raw_ptr['a]) -> raw_ptr['b]
val addr : ('a&) -> raw_ptr['a]
val funptr : ('a) -> raw_ptr[unit]
val clsptr : ('a) -> raw_ptr[unit]

let nullptr = __unsafe_nullptr
let get = __unsafe_ptr_get
Expand Down
2 changes: 1 addition & 1 deletion test/autogen.t/copy_array.smu
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
let a : array(u8) = []
let a : array[u8] = []
let b& = copy(a)
array/push(&b, !'2')
2 changes: 1 addition & 1 deletion test/autogen.t/rc.smu
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let _ = copy(thing)
let variant = rc/create(!#some("string"))
copy(variant).ignore()

type thing = { a : option(array(int)) }
type thing = { a : option[array[int]] }
let record& = rc/create(!{a = #some([1])})

&rc/get(record) = { a = #none }
Loading

0 comments on commit 19d7d77

Please sign in to comment.