Skip to content

Commit

Permalink
WIP parse type.
Browse files Browse the repository at this point in the history
  • Loading branch information
sigilante committed Jan 20, 2025
1 parent 61b135d commit ebd5e4a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 31 deletions.
Binary file modified assets/jocktest.jam
Binary file not shown.
118 changes: 101 additions & 17 deletions hoon/lib/jock.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
%func
%lambda
%protocol
%type
%new
%if
%else
%crash
Expand Down Expand Up @@ -140,7 +142,7 @@
++ tagged-keyword (stag %keyword keyword)
++ keyword
%- perk
:~ %let %func %lambda %protocol
:~ %let %func %lambda %protocol %type %new
%if %else %crash %assert
%object %compose %loop %defer
%recur %match %eval %with %this
Expand Down Expand Up @@ -216,6 +218,7 @@
$% [%let type=jype val=jock next=jock]
[%func type=jype body=jock next=jock]
[%protocol type=jock body=(map term jype) next=jock]
[%type name=jype arms=(map term jock) next=jock]
[%edit limb=(list jlimb) val=jock next=jock]
[%increment val=jock]
[%cell-check val=jock]
Expand Down Expand Up @@ -275,7 +278,7 @@
+$ jype
$+ jype
$: $^([p=jype q=jype] p=jype-leaf)
name=term
name=cord
==
:: Jype bottomed-out types
+$ jype-leaf
Expand Down Expand Up @@ -333,6 +336,7 @@
++ match-jock
|= =tokens
^- [jock (list token)]
~& >> match-jock+tokens
?: =(~ tokens)
~|("expect jock. token: ~" !!)
=^ jock tokens
Expand Down Expand Up @@ -400,6 +404,27 @@
%punctuator (match-start-punctuator tokens)
%type !! ::(match-metatype tokens) :: shouldn't reach this way
==
:: match jocks with no terminating jock (i.e. func bodies)
++ match-jock-body
|= [=tokens end=jpunc]
^- [jock (list token)]
?: =(~ tokens)
~|("expect jock. token: ~" !!)
?: (has-punctuator -.tokens end)
~& %hello
!!
=^ jock tokens
?- -<.tokens
%literal
:: TODO: check if we're in a compare
(match-literal tokens)
::
%name (match-start-name tokens)
%keyword (match-keyword tokens)
%punctuator (match-start-punctuator tokens)
%type !! ::(match-metatype tokens) :: shouldn't reach this way
==
[jock tokens]
::
++ match-trait
|= =tokens
Expand Down Expand Up @@ -566,6 +591,7 @@
[[%compare [%limb limbs] %'==' b] tokens]
=^ val tokens
(match-inner-jock +.tokens)
~& match-start-name+tokens
?> (got-punctuator -.tokens %';')
=^ jock tokens
(match-jock +.tokens)
Expand Down Expand Up @@ -641,17 +667,21 @@
=. tokens +>.tokens
=^ out tokens
(match-jype tokens)
~& 'here'
=^ body tokens
(match-block [tokens %'{' %'}'] match-jock)
(match-block [tokens %'{' %'}'] (curr match-jock-body %'}'))
~& 'here2'
?> (got-punctuator -.tokens %';')
:: ?> (got-punctuator +<.tokens %';')
=^ next tokens
(match-jock +.tokens)
(match-jock +>.tokens)
=. type
:- [%core [%& [`inp out]] ~]
name.type
=. body
:- %lambda
[[`inp out] body ~]
~& func+tokens
[[%func type body next] tokens]
::
:: lambda (b:@) -> @ {+(b)}(23);
Expand All @@ -678,9 +708,9 @@
=/ type -.tokens
?> ?=(%type -.type)
:: mask out reserved types
?: =([%type 'List'] type) !!
?: =([%type 'Set'] type) !!
?: =([%type 'Map'] type) !!
?: =([%type 'List'] type) ~|('Shadowing reserved type List is not allowed.' !!)
?: =([%type 'Set'] type) ~|('Shadowing reserved type Set is not allowed.' !!)
?: =([%type 'Map'] type) ~|('Shadowing reserved type Map is not allowed.' !!)
=. tokens +.tokens
?> (got-punctuator -.tokens %'{')
=| arg=(map term jype)
Expand All @@ -698,6 +728,62 @@
(match-jock tokens)
[`jock`[%protocol `jock`[%limb ~[[%name +.type]]] `(map term jype)`body `jock`next] tokens]
::
:: [%type name=cord arms=(map term jock) next=jock]
%type
=^ jype tokens
(match-jype tokens)
:: mask out reserved types
?: =([%type 'List'] name.jype) ~|('Shadowing reserved type List is not allowed.' !!)
?: =([%type 'Set'] name.jype) ~|('Shadowing reserved type Set is not allowed.' !!)
?: =([%type 'Map'] name.jype) ~|('Shadowing reserved type Map is not allowed.' !!)
:: TODO check `implements`
~& name+jype
~& tokkens+[+.tokens]
?> (got-punctuator -.tokens %'{')
=| arms=(map term jock)
=. tokens +.tokens
=^ arms tokens
|-
~& arms+arms
~& tokkkens+tokens
?: (has-punctuator -.tokens %'}')
[arms +.tokens]
:: :: ?> (has-keyword -.tokens %func)
:: =^ jok tokens
:: (match-jock tokens)
:: ~& >>> jok+jok
:: ?> (got-punctuator -.tokens %';')
:: =/ jok ;;([%let type=^jype val=jock next=jock] jok)
=^ type tokens
(match-jype tokens)
=^ inp tokens
(match-block [tokens %'((' %')'] match-jype)
?> (got-punctuator -.tokens %'-')
?> (got-punctuator +<.tokens %'>')
=. tokens +>.tokens
=^ out tokens
(match-jype tokens)
=. type
:- [%core [%& [`inp out]] ~]
name.type
~& there+type
=^ body tokens
:: (match-block [tokens %'{' %'}'] (curr match-jock-body %';'))
(match-block [tokens %'{' %'}'] match-jock)
~& 'there2'
?> (got-punctuator -.tokens %';')
=. body
:- %lambda
[[`inp out] body ~]
~& lunc+tokens
:: [[%func type body next] tokens]
%= $
arms (~(put by arms) name.type [%func type body *jock])
tokens +.tokens
==
:_ tokens
[%type jype arms *jock]
::
:: if (a < b) { +(a) } else { +(b) }
:: [%if cond=jock then=jock after-if=after-if-expression]
%if
Expand Down Expand Up @@ -733,16 +819,8 @@
%object
=/ has-name ?=(^ (get-name -.tokens))
=/ cor-name (fall (get-name -.tokens) %$)
~& name+cor-name
~& tokkens+[+.tokens]
=? tokens has-name +.tokens
~& name+cor-name
~& tokens+tokens
=/ validated=? (has-punctuator -.tokens %':')
=/ has-type ?=(^ (get-name +<.tokens))
=/ cor-type (fall (get-name +<.tokens) %$)
~& type+cor-type
=? tokens has-type +>.tokens
=? tokens has-name
+.tokens
?> (got-punctuator -.tokens %'{')
=. tokens +.tokens
=^ core tokens
Expand Down Expand Up @@ -998,8 +1076,10 @@
++ match-block
|* [[=tokens start=jpunc end=jpunc] gate=$-(tokens [* tokens])]
?> (got-punctuator -.tokens start)
~& match-block+tokens
=^ output tokens
(gate +.tokens)
~& match-block-out+tokens
?> (got-punctuator -.tokens end)
[output +.tokens]
::
Expand Down Expand Up @@ -1359,6 +1439,10 @@
:: :: |=([k=term v=jype] =+([val val-jyp]=^$(jyp v) [key+k val+val jyp+val-jyp]))
=+ [nex nex-jyp]=$(j next.j)
[nex nex-jyp]
::
%type
=+ [nex nex-jyp]=$(j next.j)
[nex nex-jyp]
::
%edit
=/ [typ=jype axi=@]
Expand Down
7 changes: 7 additions & 0 deletions hoon/lib/test-jock.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
/* example-atom %jock /lib/tests/example-atom/jock
/* sets %jock /lib/tests/sets/jock
/* protocol %jock /lib/tests/protocol/jock
/* type-point %jock /lib/tests/type-point/jock
::
/* test-let-edit %hoon /tests/lib/let-edit/hoon
/* test-let-inner-exp %hoon /tests/lib/let-inner-exp/hoon
Expand All @@ -51,6 +52,8 @@
/* test-match-type %hoon /tests/lib/match-type/hoon
/* test-example-atom %hoon /tests/lib/example-atom/hoon
/* test-sets %hoon /tests/lib/sets/hoon
:: /* test-protocol %hoon /tests/lib/protocol/hoon
:: /* test-type-point %hoon /tests/lib/type-point/hoon
::
|%
++ list-jocks
Expand Down Expand Up @@ -81,6 +84,7 @@
[%example-atom q.example-atom] :: 22
[%sets q.sets] :: 23
[%protocol q.protocol] :: 24
[%type-point q.type-point] :: 25
==
::
++ test-jocks
Expand Down Expand Up @@ -163,6 +167,9 @@
:: [%test-protocol-tokens test-tokenize:test-protocol]
:: [%test-protocol-jeam test-jeam:test-protocol]
:: [%test-protocol-mint test-mint:test-protocol]
:: [%test-type-point-tokens test-tokenize:test-type-point]
:: [%test-type-point-jeam test-jeam:test-type-point]
:: [%test-type-point-mint test-mint:test-type-point]
==
::
++ parse
Expand Down
24 changes: 10 additions & 14 deletions hoon/lib/tests/protocol.jock
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
protocol Arithmetic {
func add(#) -> #;
func sub(#) -> #;
add(#) -> #;
sub(#) -> #;
};

object Point: Arithmetic {
p:(x:@ y:@);
type Point(x:@ y:@) implements Arithmetic {
init(q:(x:@ y:@)) -> Point {
p.x = q.x;
p.y = q.y;
}
x = q.x;
y = q.y;
};
add(q:Point) -> Point {
Point(((p.x + q.x) (p.y + q.y)))
new Point(((x + q.x) (y + q.y)));
};
sub(q:Point) -> Point {
Point(((p.x - q.x) (p.y - q.y)))
new Point(((x - q.x) (y - q.y)));
};
};

5;

/*let origin:Point = (0 0);
let vector:Point = (1 2);
let origin:Point = new Point(0 0);
let vector:Point = new Point(1 2);

origin.add(vector)
*/

0 comments on commit ebd5e4a

Please sign in to comment.