diff --git a/ROADMAP.md b/ROADMAP.md index a4cf602..df507de 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -72,6 +72,10 @@ Implement a native `Map` type with syntax support. Change to left-to-right association to minimize surprise. +## Operator overloading + +Allow a standard battery of `class`-affiliated arms to process values. + ## `Path` type Implement a `List(String)` type with syntax support. diff --git a/common/hoon/lib/hoon.hoon b/common/hoon/lib/hoon.hoon index 1aeca0e..a299628 100644 --- a/common/hoon/lib/hoon.hoon +++ b/common/hoon/lib/hoon.hoon @@ -639,4 +639,12 @@ =+ d=(get b) (~(put by a) b (~(put in d) c)) -- +:: +:: String support operations +:: +++ concat + |= [p=@t q=@t] + ^- @t + (cat 3 p q) +:: -- diff --git a/common/hoon/lib/jock.hoon b/common/hoon/lib/jock.hoon index 7bc966e..fcf3ab5 100644 --- a/common/hoon/lib/jock.hoon +++ b/common/hoon/lib/jock.hoon @@ -8,8 +8,6 @@ :: +>+>+ jeam door, in middle :: +>+>+>+ parse core, next :: -:: We automatically supply a copy of Hoon into the namespace as an FFI because -:: so many built-in tools like arithmetic depend on it. =< |_ libs=(map term cord) ++ tokenize |= txt=@ @@ -125,7 +123,14 @@ ++ tokenize =| fun=?(%.y %.n) |% - ++ string (stag %string (cook crip (ifix [soq soq] (star ;~(less soq prn))))) + ++ string %+ stag + %string + %+ cook + crip + ;~ pose + (ifix [soq soq] (star ;~(less soq prn))) + (ifix [doq doq] (star ;~(less doq prn))) + == ++ number (stag %number dem:ag) ++ hexadecimal (stag %hexadecimal ;~(pfix (jest %'0x') hex)) ++ loobean @@ -516,6 +521,7 @@ %type (match-start-name tokens) == :: match jocks with no terminating jock (i.e. func bodies) +:: use with +curr ++ match-jock-body |= [=tokens end=jpunc] ^- [jock (list token)] @@ -523,16 +529,18 @@ ~|("expect jock. token: ~" !!) ?: (has-punctuator -.tokens end) !! =^ 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-start-name tokens) - == + (match-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-start-name tokens) + :: == + ?> (has-punctuator -.tokens end) [jock tokens] :: ++ match-trait @@ -1922,6 +1930,7 @@ ?+ -.func.j ~|('must call a limb' !!) %limb =/ old-jyp jyp + ~& call-limb+j ~| %call-limb =/ limbs=(list jlimb) p.func.j ?> ?=(^ limbs) @@ -1961,9 +1970,12 @@ :: We have to +slam the gate into the Hoon library, :: thus two separate wings. ~| %call-case-6 + ~& 'here' ?> ?=(^ limbs) ?~ arg.j ~|("expect function argument" !!) + ~& here2+u.arg.j =+ [val val-jyp]=$(j u.arg.j) + ~& val+val :: Construct the AST for the Hoon RPC using the bunt for now. =+ ast=(j2h ljl ~) ?> ?=(%hoon -<.typ) @@ -2183,36 +2195,51 @@ :: %operator ~| %operator - :_ [%atom %number %.n]^%$ + :_ ?+ a.j + ~|('operator: no valid result type' !!) + :: + [%atom [[%number p=@ud] q=?]] + [%atom %number %.n]^%$ + :: + [%atom [[%string p=cord] q=?]] + [%atom %string %.n]^%$ + :: + == + :: TODO support unary operands + ?~ b.j !! + =/ b u.b.j ::$(j u.b.j) ?- op.j %'+' - ?~ b.j !! - =/ j=jock [%call [%limb p=~[[%name %hoon] [%name %add]]] arg=`[a.j u.b.j]] + =/ j=jock + ?+ a.j + ~|('binary + requires two arguments' !!) + :: + [%atom [[%number p=@ud] q=?]] + [%call [%limb p=~[[%name %hoon] [%name %add]]] arg=`[a.j b]] + :: + [%atom [[%string p=cord] q=?]] + [%call [%limb p=~[[%name %hoon] [%name %concat]]] arg=`[a.j b]] + == -:$(j j) :: %'-' - ?~ b.j !! - =/ j=jock [%call [%limb p=~[[%name %hoon] [%name %sub]]] arg=`[a.j u.b.j]] + =/ j=jock [%call [%limb p=~[[%name %hoon] [%name %sub]]] arg=`[a.j b]] -:$(j j) :: %'*' - ?~ b.j !! - =/ j=jock [%call [%limb p=~[[%name %hoon] [%name %mul]]] arg=`[a.j u.b.j]] + =/ j=jock [%call [%limb p=~[[%name %hoon] [%name %mul]]] arg=`[a.j b]] -:$(j j) :: %'/' - ?~ b.j !! - =/ j=jock [%call [%limb p=~[[%name %hoon] [%name %div]]] arg=`[a.j u.b.j]] + =/ j=jock [%call [%limb p=~[[%name %hoon] [%name %div]]] arg=`[a.j b]] -:$(j j) :: %'%' - ?~ b.j !! - =/ j=jock [%call [%limb p=~[[%name %hoon] [%name %mod]]] arg=`[a.j u.b.j]] + =/ j=jock [%call [%limb p=~[[%name %hoon] [%name %mod]]] arg=`[a.j b]] -:$(j j) :: %'**' - ?~ b.j !! - =/ j=jock [%call [%limb p=~[[%name %hoon] [%name %pow]]] arg=`[a.j u.b.j]] + =/ j=jock [%call [%limb p=~[[%name %hoon] [%name %pow]]] arg=`[a.j b]] -:$(j j) :: == diff --git a/crates/jockc/hoon/main.hoon b/crates/jockc/hoon/main.hoon index f06e94f..a2837e6 100644 --- a/crates/jockc/hoon/main.hoon +++ b/crates/jockc/hoon/main.hoon @@ -42,7 +42,7 @@ =/ args (turn args.cau (cury scot %ud)) =/ code (preprocess text.cau args) =/ libs `(map term cord)`(malt libs-list.cau) - ~& libs+[~(tap by libs)] + ~& libs+[~(key by libs)] ~& code+[code] ~& parse+(parse:~(. runner libs) code) ~& jeam+(jeam:~(. runner libs) code) diff --git a/crates/jockt/hoon/lib/test-jock.hoon b/crates/jockt/hoon/lib/test-jock.hoon index 1b30ace..4b43169 100644 --- a/crates/jockt/hoon/lib/test-jock.hoon +++ b/crates/jockt/hoon/lib/test-jock.hoon @@ -36,6 +36,7 @@ /* hoon-alias %jock /lib/tests/hoon-alias/jock /* fib %jock /lib/tests/fib/jock /* lists-indexing %jock /lib/tests/lists-indexing/jock +/* strings %jock /lib/tests/strings/jock :: /* test-let-edit %hoon /tests/lib/let-edit/hoon /* test-let-inner-exp %hoon /tests/lib/let-inner-exp/hoon @@ -73,6 +74,7 @@ :: /* test-hoon-alias %hoon /tests/lib/hoon-alias/hoon :: /* test-fib %hoon /tests/lib/fib/hoon :: /* test-lists-indexing %hoon /tests/lib/lists-indexing/hoon +:: /* test-strings %hoon /tests/lib/strings/hoon :: |_ libs=(map term cord) ++ list-jocks @@ -113,7 +115,8 @@ [%hoon-alias q.hoon-alias] :: 33 [%fib q.fib] :: 34 [%lists-indexing q.lists-indexing] :: 35 - == + [%strings q.strings] :: 36 + == :: ++ test-jocks ~+ @@ -258,6 +261,10 @@ :: [%test-lists-indexing-jeam test-jeam:test-lists-indexing] :: [%test-lists-indexing-mint test-mint:test-lists-indexing] :: [%test-lists-indexing-nock test-nock:test-lists-indexing] + :: [%test-strings-tokens test-tokenize:test-strings] + :: [%test-strings-jeam test-jeam:test-strings] + :: [%test-strings-mint test-mint:test-strings] + :: [%test-strings-nock test-nock:test-strings] == :: ++ parse diff --git a/crates/jockt/hoon/lib/tests/strings.jock b/crates/jockt/hoon/lib/tests/strings.jock new file mode 100644 index 0000000..61f69f0 --- /dev/null +++ b/crates/jockt/hoon/lib/tests/strings.jock @@ -0,0 +1,7 @@ +// Define a function that returns a greeting +func greet(name: String) -> String { + 'Hello, ' + name + '!' +}; + +print(greet("Zorp")); +greet('Zorp')