Skip to content

Commit 50431f8

Browse files
committed
stdlib: new floats and !-operator #22 #24
1 parent b44d761 commit 50431f8

17 files changed

+177
-28
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## v0.0.17-next
44

5+
- stdlib: Float-support! #24
6+
- stdlib: `!` operator #22
7+
58
## v0.0.16
69

710
- fix: undeclared enum case error due to file order within a module

ast/expr-float.go

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ func (e ExprFloat) Meta() *MetaExpr {
1212
return e.MetaInfo
1313
}
1414

15+
func MakeExprFloat(literal float64, source *Source) *ExprFloat {
16+
return &ExprFloat{
17+
Literal: literal,
18+
MetaInfo: &MetaExpr{
19+
Source: source,
20+
},
21+
}
22+
}
23+
1524
func (e ExprFloat) EnumerateNestedDecls(enumerate func(interface{}, []Decl)) {
1625
// no nested decls
1726
}

ast/expr-operator-unary.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var _ Expr = ExprOperatorUnary{}
44

55
type ExprOperatorUnary struct {
66
Operator OperatorUnary
7-
Expr *Expr
7+
Expr Expr
88

99
MetaInfo *MetaExpr
1010
}
@@ -13,6 +13,16 @@ func (e ExprOperatorUnary) Meta() *MetaExpr {
1313
return e.MetaInfo
1414
}
1515

16+
func MakeExprOperatorUnary(operator OperatorUnary, expr Expr, source *Source) *ExprOperatorUnary {
17+
return &ExprOperatorUnary{
18+
Operator: operator,
19+
Expr: expr,
20+
MetaInfo: &MetaExpr{
21+
Source: source,
22+
},
23+
}
24+
}
25+
1626
func (e ExprOperatorUnary) EnumerateNestedDecls(enumerate func(interface{}, []Decl)) {
17-
(*e.Expr).EnumerateNestedDecls(enumerate)
27+
e.Expr.EnumerateNestedDecls(enumerate)
1828
}

go.mod

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ module github.com/vknabel/lithia
33
go 1.16
44

55
require (
6+
github.com/gorilla/websocket v1.5.0 // indirect
67
github.com/muesli/coral v1.0.0
7-
github.com/petermattis/goid v0.0.0-20220111183729-e033e1e0bdb5 // indirect
8-
github.com/smacker/go-tree-sitter v0.0.0-20211116060328-db7fde9b5e82
8+
github.com/petermattis/goid v0.0.0-20220302125637-5f11c28912df // indirect
9+
github.com/smacker/go-tree-sitter v0.0.0-20220209044044-0d3022e933c3
910
github.com/tliron/glsp v0.0.0-20210824162824-d103e5701036
10-
github.com/tliron/kutil v0.1.52
11-
github.com/vknabel/tree-sitter-lithia v0.0.0-20220121161404-ed4529b7c21c
12-
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a // indirect
11+
github.com/tliron/kutil v0.1.55
12+
github.com/vknabel/tree-sitter-lithia v0.1.2
13+
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
1314
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
1415
)

go.sum

+18-15
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1/go.mod h1:cyGadeNE
311311
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
312312
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
313313
github.com/dop251/goja v0.0.0-20210810150349-acd0507c3d6f/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
314-
github.com/dop251/goja v0.0.0-20220124171016-cfb079cdc7b4/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
314+
github.com/dop251/goja v0.0.0-20220214123719-b09a6bfa842f/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
315315
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
316316
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
317317
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
@@ -500,8 +500,9 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
500500
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
501501
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
502502
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
503-
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
504503
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
504+
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
505+
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
505506
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
506507
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
507508
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
@@ -597,6 +598,7 @@ github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQ
597598
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
598599
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
599600
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
601+
github.com/kortschak/utter v1.5.0/go.mod h1:vSmSjbyrlKjjsL71193LmzBOKgwePk9DH6uFaWHIInc=
600602
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
601603
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
602604
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -731,8 +733,8 @@ github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCko
731733
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
732734
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
733735
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5/go.mod h1:jvVRKCrJTQWu0XVbaOlby/2lO20uSCHEMzzplHXte1o=
734-
github.com/petermattis/goid v0.0.0-20220111183729-e033e1e0bdb5 h1:AdVIf7YN+kn8QAapO6IQHtHKSBeSiNgkSS8Yai/xMmo=
735-
github.com/petermattis/goid v0.0.0-20220111183729-e033e1e0bdb5/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4=
736+
github.com/petermattis/goid v0.0.0-20220302125637-5f11c28912df h1:/B1Q9E4W1cmiwPQfC2vymWL7FXHCEsUzg8Rywl5avtQ=
737+
github.com/petermattis/goid v0.0.0-20220302125637-5f11c28912df/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4=
736738
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
737739
github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
738740
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -806,9 +808,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
806808
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
807809
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
808810
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
809-
github.com/smacker/go-tree-sitter v0.0.0-20210728070821-d2f3e69b145a/go.mod h1:EiUuVMUfLQj8Sul+S8aKWJwQy7FRYnJCO2EWzf8F5hk=
810-
github.com/smacker/go-tree-sitter v0.0.0-20211116060328-db7fde9b5e82 h1:e17/Q2AF05ZITfj9y/dqOU+ceEXOeYzKGGuIXNsDlxM=
811-
github.com/smacker/go-tree-sitter v0.0.0-20211116060328-db7fde9b5e82/go.mod h1:EiUuVMUfLQj8Sul+S8aKWJwQy7FRYnJCO2EWzf8F5hk=
811+
github.com/smacker/go-tree-sitter v0.0.0-20220209044044-0d3022e933c3 h1:WrsSqod9T70HFyq8hjL6wambOKb4ISUXzFUuNTJHDwo=
812+
github.com/smacker/go-tree-sitter v0.0.0-20220209044044-0d3022e933c3/go.mod h1:EiUuVMUfLQj8Sul+S8aKWJwQy7FRYnJCO2EWzf8F5hk=
812813
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
813814
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
814815
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
@@ -864,8 +865,8 @@ github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ
864865
github.com/tliron/glsp v0.0.0-20210824162824-d103e5701036 h1:mydMHEQPtC4DlMtDTT4WG/jhkSB3+NX4X3uJtdZ3Gk4=
865866
github.com/tliron/glsp v0.0.0-20210824162824-d103e5701036/go.mod h1:JkdOZOy+1znsGY8oa1zwT00kBsiNgL5QbcrW5JiPrIM=
866867
github.com/tliron/kutil v0.1.48/go.mod h1:JNExME6WabMoU/TMzjffPPXxQjSdRQuIVdUM8aBxNuw=
867-
github.com/tliron/kutil v0.1.52 h1:h4MQSaoutWYYj5/T/K/GOYEKeLsoo8D3vlTW0f6NUtk=
868-
github.com/tliron/kutil v0.1.52/go.mod h1:MQI/OQWJSUSaSbJjFAl7zlt7Q1nrl6sxuMtIiRxECz0=
868+
github.com/tliron/kutil v0.1.55 h1:VUBwGLxnATLeaCFDhdNFRmIyIhRVwOjlw46YWIivTm4=
869+
github.com/tliron/kutil v0.1.55/go.mod h1:TdquwxFN0PBuw/avcFHIFFZZMXINaY5/fB7g/hM3UY4=
869870
github.com/tliron/yamlkeys v1.3.5/go.mod h1:8kJ1A/1s3p/I3MQUAbtv72dPEyQGoh0ZkQp0UAkABBo=
870871
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
871872
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
@@ -884,8 +885,10 @@ github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852/go.mod h1:tw
884885
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI=
885886
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
886887
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
887-
github.com/vknabel/tree-sitter-lithia v0.0.0-20220121161404-ed4529b7c21c h1:Ad65423WfQEhP5lrDBg7s3rRWEHKlmUWl4nxtYWzWmo=
888-
github.com/vknabel/tree-sitter-lithia v0.0.0-20220121161404-ed4529b7c21c/go.mod h1:/UywjzJ0Jf623Jj+XWOSepHZ182EUBoyttg+MpS6MbE=
888+
github.com/vknabel/tree-sitter-lithia v0.1.1 h1:tjSTgeiBZjqihWd0aEIKJpsx4TzuPArZjaqogdzOWpM=
889+
github.com/vknabel/tree-sitter-lithia v0.1.1/go.mod h1:2cs81uybH+bfnyNOrCaBRNEKS7X4okL074o9VGDnhdc=
890+
github.com/vknabel/tree-sitter-lithia v0.1.2 h1:b+fOu4eFjc5gyqjfeymLaKJ5B84LEZAlCsLwD9wZW+E=
891+
github.com/vknabel/tree-sitter-lithia v0.1.2/go.mod h1:2cs81uybH+bfnyNOrCaBRNEKS7X4okL074o9VGDnhdc=
889892
github.com/willf/bitset v1.1.11-0.20200630133818-d5bec3311243/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
890893
github.com/willf/bitset v1.1.11/go.mod h1:83CECat5yLh5zVOf4P1ErAgKA5UDvKtgyUABdr3+MjI=
891894
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
@@ -973,8 +976,8 @@ golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm
973976
golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
974977
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
975978
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
976-
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 h1:71vQrMauZZhcTVK6KdYM+rklehEEwb3E+ZhaE5jrPrE=
977-
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
979+
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 h1:f+lwQ+GtmgoY+A2YaQxlSOnDjXcQ7ZRLWOHbC6HtRqE=
980+
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
978981
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
979982
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
980983
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -1213,8 +1216,8 @@ golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBc
12131216
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12141217
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12151218
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1216-
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a h1:ppl5mZgokTT8uPkmYOyEUmPTr3ypaKkg5eFOGrAmxxE=
1217-
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1219+
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs=
1220+
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12181221
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
12191222
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
12201223
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

langsrv/handler-text-document-semantic-tokens-full.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func textDocumentSemanticTokensFull(context *glsp.Context, params *protocol.Sema
4949
(unary_expression operator: (["!"]) @operator)
5050
5151
(parameter_list (identifier) @variable.parameter)
52-
(number_literal) @constant.numeric
52+
(int_literal) @constant.numeric
53+
(float_literal) @constant.numeric
5354
(comment) @comment
5455
(function_declaration name: (identifier) @function)
5556
(let_declaration name: (identifier) @variable)

parser/node-types.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ var (
2424
TYPE_NODE_STRING_LITERAL = "string_literal"
2525
TYPE_NODE_ESCAPE_SEQUENCE = "escape_sequence"
2626
TYPE_NODE_GROUP_LITERAL = "group_literal"
27-
TYPE_NODE_NUMBER_LITERAL = "number_literal"
27+
TYPE_NODE_INT_LITERAL = "int_literal"
28+
TYPE_NODE_FLOAT_LITERAL = "float_literal"
2829
TYPE_NODE_ARRAY_LITERAL = "array_literal"
2930
TYPE_NODE_FUNCTION_LITERAL = "function_literal"
3031
TYPE_NODE_PARAMETER_LIST = "parameter_list"

parser/parse-expr-float.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package parser
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/vknabel/lithia/ast"
7+
)
8+
9+
func (fp *FileParser) ParseFloatExpr() (*ast.ExprFloat, []SyntaxError) {
10+
literal := fp.Node.Content(fp.Source)
11+
integer, err := strconv.ParseFloat(literal, 64)
12+
if err != nil {
13+
return nil, []SyntaxError{*fp.SyntaxErrorOrConvert(err)}
14+
}
15+
return ast.MakeExprFloat(integer, fp.AstSource()), nil
16+
}

parser/parse-expr-unary.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import (
44
"github.com/vknabel/lithia/ast"
55
)
66

7-
func (fp *FileParser) ParseUnaryExpr() (*ast.ExprInt, []SyntaxError) {
8-
return nil, []SyntaxError{fp.SyntaxErrorf("unimplemented")}
7+
func (fp *FileParser) ParseUnaryExpr() (*ast.ExprOperatorUnary, []SyntaxError) {
8+
if fp.Node.NamedChildCount() != 1 {
9+
return nil, []SyntaxError{fp.SyntaxErrorf("expected one child, got %d", fp.Node.NamedChildCount())}
10+
}
11+
operator := fp.Node.ChildByFieldName("operator").Content(fp.Source)
12+
13+
exprP := fp.SameScopeChildParser(fp.Node.NamedChild(0))
14+
expr, errs := exprP.ParseExpression()
15+
if len(errs) > 0 {
16+
return nil, errs
17+
}
18+
return ast.MakeExprOperatorUnary(ast.OperatorUnary(operator), expr, fp.AstSource()), nil
919
}

parser/parse-expr.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,20 @@ func (fp *FileParser) ParseExpressionIfGiven() (ast.Expr, []SyntaxError) {
6464
} else {
6565
return expr, errs
6666
}
67-
case TYPE_NODE_NUMBER_LITERAL:
67+
case TYPE_NODE_INT_LITERAL:
6868
expr, errs := fp.ParseIntExpr()
6969
if expr == nil {
7070
return nil, errs
7171
} else {
7272
return expr, errs
7373
}
74+
case TYPE_NODE_FLOAT_LITERAL:
75+
expr, errs := fp.ParseFloatExpr()
76+
if expr == nil {
77+
return nil, errs
78+
} else {
79+
return expr, errs
80+
}
7481
case TYPE_NODE_ARRAY_LITERAL:
7582
expr, errs := fp.ParseExprArray()
7683
if expr == nil {

runtime/evaluatable-expr.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,16 @@ func (e EvaluatableExpr) EvaluateExprOperatorBinary(expr ast.ExprOperatorBinary)
166166
}
167167

168168
func (e EvaluatableExpr) EvaluateExprOperatorUnary(expr ast.ExprOperatorUnary) (RuntimeValue, *RuntimeError) {
169-
panic("unary expressions https://github.com/vknabel/lithia/issues/22")
169+
impl, err := e.Context.unaryOperatorFunction(string(expr.Operator))
170+
if err != nil {
171+
return nil, err.CascadeCall(nil, expr)
172+
}
173+
eval := MakeEvaluatableExpr(e.Context, expr.Expr)
174+
result, err := impl(eval)
175+
if err != nil {
176+
return nil, err.CascadeCall(nil, expr)
177+
}
178+
return result, nil
170179
}
171180

172181
func (e EvaluatableExpr) EvaluateExprString(expr ast.ExprString) (RuntimeValue, *RuntimeError) {
File renamed without changes.

runtime/operators-unary.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package runtime
2+
3+
func (ex *InterpreterContext) unaryOperatorFunction(operator string) (func(Evaluatable) (RuntimeValue, *RuntimeError), *RuntimeError) {
4+
switch operator {
5+
case "!":
6+
return func(expr Evaluatable) (RuntimeValue, *RuntimeError) {
7+
value, err := expr.Evaluate()
8+
if err != nil {
9+
return nil, err
10+
}
11+
flag, err := ex.boolFromRuntimeValue(value)
12+
if err != nil {
13+
return nil, err
14+
}
15+
return ex.boolToRuntimeValue(!flag)
16+
}, nil
17+
default:
18+
return nil, NewRuntimeErrorf("unknown binary operator: %s", operator)
19+
}
20+
}
21+
22+
func (ex *InterpreterContext) boolFromRuntimeValue(value RuntimeValue) (bool, *RuntimeError) {
23+
trueRef := MakeRuntimeTypeRef("True", "prelude")
24+
isTrue, err := trueRef.HasInstance(ex.interpreter, value)
25+
if err != nil {
26+
return false, NewRuntimeError(err)
27+
} else if isTrue {
28+
return true, nil
29+
}
30+
falseRef := MakeRuntimeTypeRef("True", "prelude")
31+
_, err = falseRef.HasInstance(ex.interpreter, value)
32+
if err != nil {
33+
return false, NewRuntimeError(err)
34+
} else {
35+
return false, nil
36+
}
37+
}

runtime/runtime-error-helpers.go

+12
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ func ReportBinaryOperatorOnlySupportsType(operator string, supportedTypes []Runt
5959
gotValue.String(),
6060
)
6161
}
62+
func ReportUnatyOperatorOnlySupportsType(operator string, supportedTypes []RuntimeTypeRef, gotValue RuntimeValue) *RuntimeError {
63+
supportedTypesNames := []string{}
64+
for _, supported := range supportedTypes {
65+
supportedTypesNames = append(supportedTypesNames, string(supported.Name))
66+
}
67+
return NewTypeErrorf(
68+
"unary operator %q only supports %s; value given: %q",
69+
operator,
70+
strings.Join(supportedTypesNames, ", "),
71+
gotValue.String(),
72+
)
73+
}
6274

6375
func ReportNonExhaustiveTypeSwitch(
6476
enumDecl PreludeEnumDecl,

stdlib/prelude/prelude_t/bool.lithia

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ test "binary || operator", { fail =>
77
unless True || True, fail "(True || True) == True"
88
}
99

10+
test "unary ! operator", { fail =>
11+
when !True, fail "!True == False"
12+
unless !False, fail "!False == True"
13+
}
14+
1015
test "binary && operator", { fail =>
1116
when False && False, fail "(False && False) == False"
1217
when True && False, fail "(True && False) == False"

stdlib/stdlib-tests.lithia

+20
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ test "int comparisions", { fail =>
5353
unless (incr 1) == 2, fail "(incr 1) == 2"
5454
}
5555

56+
test "float comparisions", { fail =>
57+
unless 1.0 == 1.0, fail "1.0 == 1.0"
58+
unless 4.2 == 4.20, fail "4.2 == 4.20"
59+
unless 0.0 == 0.0, fail "0.0 == 0.0"
60+
unless 0.37 != 0.42, fail "0.37 != 0.42"
61+
unless 1.3 != 4.2, fail "1.3 != 4.2"
62+
when 1.2 == 2.3, fail "!(1.2 == 2.3)"
63+
64+
unless 1.1 + 1.2 == 2.3, fail "1.1 + 1.2 == 2.3"
65+
unless 3.01 * 2 + 1 == 7.02, fail "3.01 * 2 + 1 == 7.02"
66+
67+
unless 4.2 - 0.1 < 4.2, fail "4.2 - 0.1 < 4.2"
68+
when 4.2 < 4.2, fail "4.2 < 4.2"
69+
70+
unless 1.1 * 1 == 1.1, fail "1.1 * 1 == 1.1"
71+
72+
func incr { i => i + 1 }
73+
unless (incr 1.3) == 2.3, fail "(incr 1.3) == 2.3"
74+
}
75+
5676
test "lists.replicate", { fail =>
5777
when (lists.replicate 0, 42) != [], fail "zero elements"
5878
when (lists.replicate 1, 42) != [42], fail "one element"

stdlib/tests/internal/test-cases.lithia

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ func runTestCase { summary, testCase =>
1212

1313
let testNumber = summary.ok + summary.notOk + 1
1414
let failure = rx.Variable None
15+
print strings.concat [
16+
testNumber,
17+
" - ",
18+
testCase.title
19+
]
1520
testCase.impl { message =>
1621
let combined = with failure.current, type Optional {
1722
Some: { previous => Some strings.concat [previous.value, "; ", message] },

0 commit comments

Comments
 (0)