* [PATCH olang v2 0/2] docs: variables specification
@ 2024-04-08 4:38 Carlos Maniero
2024-04-08 4:38 ` [PATCH olang v2 1/2] docs: spec: rename program to translation-unit Carlos Maniero
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Carlos Maniero @ 2024-04-08 4:38 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero
This patchset specifies variables and constants.
Main changes in v2:
- Simplifications suggested by Johnny related to EOF
- Support all assign operators
There are two things a left outside this patchset:
1. The statement *const a += 2* is still valid at this spec.
I tried to fix it but it ends up too much complex. I'm not sure we need
to be so strict on the spec. The C spec does the same so I think we are
fine.
2. Chained assignment
The statement *a = b = c* is not valid right now. We could discuss if
this is something that we really want to incorporate in the language.
The main reason are:
- It is hard to implement and it may mess with the great expression
precedence algorithm we have right now.
- It brings complexity for the spec, the version we had at the previous
discussion the statement *return = 2* was valid.
Fun fact that does not need to be considered:
- In gcc *a = b = 2* is slower then *a = 2*, *b = 2* once it actually
makes *a = 2; b = a* requiring an extra instruction to copy the value
from *a* at the stack to a register. But it is probably solved by
enabling *-O3*
Do you think we can deal with chained assignment latter?
Carlos Maniero (2):
docs: spec: rename program to translation-unit
docs: spec: add variables and constants specification
docs/pages/language-specification.md | 32 ++++++++++++++++++++++------
1 file changed, 25 insertions(+), 7 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH olang v2 1/2] docs: spec: rename program to translation-unit
2024-04-08 4:38 [PATCH olang v2 0/2] docs: variables specification Carlos Maniero
@ 2024-04-08 4:38 ` Carlos Maniero
2024-04-08 4:38 ` [PATCH olang v2 2/2] docs: spec: add variables and constants specification Carlos Maniero
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Carlos Maniero @ 2024-04-08 4:38 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
docs/pages/language-specification.md | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/docs/pages/language-specification.md b/docs/pages/language-specification.md
index 5769d95..544741a 100644
--- a/docs/pages/language-specification.md
+++ b/docs/pages/language-specification.md
@@ -22,8 +22,7 @@ language.
```
(* Entry Point *)
-<program> ::= <ows> <function-definition> <ows> <end-of-file>
-
+<translation-unit> ::= <ows> <function-definition> <ows> <end-of-file>
(* Functions *)
<function-definition> ::= 'fn' <ws> <function-name> <ows>
<function-parameters> <ows> ':' <ows> <return-type> <ows> <function-body>
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH olang v2 2/2] docs: spec: add variables and constants specification
2024-04-08 4:38 [PATCH olang v2 0/2] docs: variables specification Carlos Maniero
2024-04-08 4:38 ` [PATCH olang v2 1/2] docs: spec: rename program to translation-unit Carlos Maniero
@ 2024-04-08 4:38 ` Carlos Maniero
2024-04-08 4:39 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-04-08 11:35 ` [PATCH olang v2 0/2] docs: variables specification Carlos Maniero
2024-04-10 7:50 ` Johnny Richard
3 siblings, 1 reply; 6+ messages in thread
From: Carlos Maniero @ 2024-04-08 4:38 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero
This commit introduces the specification for variables and constants. A
valid program under this specification is as follows:
var x: u32 = 1
const y: u32 = 2
fn main(): u32 {
var x: u32 = 1; const y: u32 = 2
return x
}
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
docs/pages/language-specification.md | 33 ++++++++++++++++++++++------
1 file changed, 26 insertions(+), 7 deletions(-)
diff --git a/docs/pages/language-specification.md b/docs/pages/language-specification.md
index 544741a..4d0eb36 100644
--- a/docs/pages/language-specification.md
+++ b/docs/pages/language-specification.md
@@ -22,24 +22,43 @@ language.
```
(* Entry Point *)
-<translation-unit> ::= <ows> <function-definition> <ows> <end-of-file>
+<translation-unit> ::= (<ows> <external-declaration> <ows> (<end-of-statement> | <end-of-file>))*
+
+<external-declaration> ::= <function-definition> | <variable-definition>
+
+(* Variables *)
+<variable-definition> ::= <variable-qualifier> <ws> <variable-name> <ows> ':' <ows> <type> (<ows> <assign-operator> <ows> <expression>)?
+<variable-qualifier> ::= 'var'
+ | 'const'
+<variable-name> ::= <identifier>
+
(* Functions *)
-<function-definition> ::= 'fn' <ws> <function-name> <ows>
-<function-parameters> <ows> ':' <ows> <return-type> <ows> <function-body>
+<function-definition> ::= 'fn' <ws> <function-name> <ows> <function-parameters> <ows> ':' <ows> <return-type> <ows> <function-body>
<function-name> ::= <identifier>
<function-parameters> ::= '(' <ows> ')'
<return-type> ::= <type>
<function-body> ::= <block>
(* Statements *)
-<block> ::= '{' <ows> <statement> <ows> (<end-of-statement>
-<ows> <statement> <ows>)* <end-of-statement>? <ows> '}'
+<block> ::= '{' <ows> <statement> <ows> (<end-of-statement> <ows> <statement> <ows>)* <end-of-statement>? <ows> '}'
<end-of-statement> ::= ';' | <line-break>
-<statement> ::= <return-statement>
+<statement> ::= <return-statement> | <variable-definition> | <assign-expression>
<return-statement> ::= 'return' <ws> <expression>
(* Expressions *)
-<expression> ::= <integer>
+<expression> ::= <integer> | <identifier>
+<assign-expression> ::= <variable-name> <ows> <assign-operator> <ows> <expression>
+<assign-operator> ::= '='
+ | '*='
+ | '/='
+ | '%='
+ | '+='
+ | '-='
+ | '<<='
+ | '>>='
+ | '&='
+ | '^='
+ | '|='
(* Identifiers *)
<type> ::= 'u32'
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [olang/patches/.build.yml] build success
2024-04-08 4:38 ` [PATCH olang v2 2/2] docs: spec: add variables and constants specification Carlos Maniero
@ 2024-04-08 4:39 ` builds.sr.ht
0 siblings, 0 replies; 6+ messages in thread
From: builds.sr.ht @ 2024-04-08 4:39 UTC (permalink / raw)
To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel
olang/patches/.build.yml: SUCCESS in 39s
[docs: variables specification][0] v2 from [Carlos Maniero][1]
[0]: https://lists.sr.ht/~johnnyrichard/olang-devel/patches/50820
[1]: mailto:carlos@maniero.me
✓ #1189894 SUCCESS olang/patches/.build.yml https://builds.sr.ht/~johnnyrichard/job/1189894
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH olang v2 0/2] docs: variables specification
2024-04-08 4:38 [PATCH olang v2 0/2] docs: variables specification Carlos Maniero
2024-04-08 4:38 ` [PATCH olang v2 1/2] docs: spec: rename program to translation-unit Carlos Maniero
2024-04-08 4:38 ` [PATCH olang v2 2/2] docs: spec: add variables and constants specification Carlos Maniero
@ 2024-04-08 11:35 ` Carlos Maniero
2024-04-10 7:50 ` Johnny Richard
3 siblings, 0 replies; 6+ messages in thread
From: Carlos Maniero @ 2024-04-08 11:35 UTC (permalink / raw)
To: Carlos Maniero, ~johnnyrichard/olang-devel
I forgot to mention, but V1 was also allowing assignments on
external-declaration. ex:
var x: u32
x = 1
fn main(): u32 {
return x
}
But, external-declarations should not allow statements, only
declarations/definitions.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH olang v2 0/2] docs: variables specification
2024-04-08 4:38 [PATCH olang v2 0/2] docs: variables specification Carlos Maniero
` (2 preceding siblings ...)
2024-04-08 11:35 ` [PATCH olang v2 0/2] docs: variables specification Carlos Maniero
@ 2024-04-10 7:50 ` Johnny Richard
3 siblings, 0 replies; 6+ messages in thread
From: Johnny Richard @ 2024-04-10 7:50 UTC (permalink / raw)
To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel
Thanks, applied!
To git.sr.ht:~johnnyrichard/olang
099eec6..7cd840e main -> main
build: https://builds.sr.ht/~johnnyrichard/job/1191820
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-04-10 6:51 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-08 4:38 [PATCH olang v2 0/2] docs: variables specification Carlos Maniero
2024-04-08 4:38 ` [PATCH olang v2 1/2] docs: spec: rename program to translation-unit Carlos Maniero
2024-04-08 4:38 ` [PATCH olang v2 2/2] docs: spec: add variables and constants specification Carlos Maniero
2024-04-08 4:39 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-04-08 11:35 ` [PATCH olang v2 0/2] docs: variables specification Carlos Maniero
2024-04-10 7:50 ` Johnny Richard
Code repositories for project(s) associated with this public inbox
https://git.johnnyrichard.com/olang.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox