public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Johnny Richard <johnny@johnnyrichard.com>
To: Carlos Maniero <carlos@maniero.me>
Cc: ~johnnyrichard/olang-devel@lists.sr.ht
Subject: Re: [PATCH olang v1 2/2] docs: spec: add variables and constants specification
Date: Wed, 27 Mar 2024 22:37:00 +0100	[thread overview]
Message-ID: <u7zpxqr57xx7ulhvzb35oinkz3ywnvjgq4i5x4b63kx4hvy6qq@7xnpwsr2w7y2> (raw)
In-Reply-To: <20240327032128.406911-3-carlos@maniero.me>

On Wed, Mar 27, 2024 at 12:21:28AM -0300, Carlos Maniero wrote:
> This commit introduces the specification for variables and constants. A
> valid program under this specification is as follows:
> 
>   var x: u32 = 1

Since this patch adds support to assignments, lets also add support to
all assignment operators like "-=" "+=" "<<=" and so on.

>   const y: u32 = 2

This patch lacks support to the following valid assignment expression
(which I think adds flexibility to the language):

    var x: u32 = a = b = c = 1
    var y: u32
    y = a = b = c = 1

>   x = 2
> 
>   fn main(): u32 {
>     var x: u32 = 1; const y: u32 = 2
>     return x
>   }
> 
> Variables and Linkage:
> ----------------------
> 
> Variables can be defined globally or within a function. Variables
> defined globally are added to the *.data* section. On the other hand,
> variables defined within a function utilize the stack. Currently, the
> specification does not provide a keyword for external linkage yet, hence
> variables cannot be accessed outside their translation unit.
> 
> Constants and Linkage:
> ----------------------
> 
> Constants behave similarly to variables. Constants defined at the file
> level are added to the *.rodata* section. Constants within functions are
> subject to semantic checks. Attempting to bypass these checks to
> reassign a global constant will result in a segmentation fault
> (SEGFAULT) and will be accepted at the function level.
> 
> Example of bypassing semantic checks in C:
> 
>   int a = 1;
>   const int b = 2;
>   int *c = &a + 1;
>   *c = 3;
>   assert(b == 3); // its true
> 
> Static variables in function level
> ----------------------------------
> 
> In C, static variables can be defined within the scope of a function.
> These variables are added to the `*.data*` segment and are only
> accessible within the function where they are defined. This is a unique
> behavior of C that some might find unusual.
> 
> However, olang does not support this feature. In olang, if you need a
> variable that retains its value across function calls (like a static
> variable in C), you must define it at the file level.
> 
> Signed-off-by: Carlos Maniero <carlos@maniero.me>
> ---
>  docs/pages/language-specification.md | 14 +++++++++++---
>  1 file changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/docs/pages/language-specification.md b/docs/pages/language-specification.md
> index 4541ba8..708b679 100644
> --- a/docs/pages/language-specification.md
> +++ b/docs/pages/language-specification.md
> @@ -24,7 +24,15 @@ language.
>  (* Entry Point *)
>  <translation-unit>    ::= <ows> (<global-statements> <end-of-statement>)*
>  
> -<global-statements>   ::= <function-definition>
> +<global-statements>   ::= <function-definition> | <variable-definition> | <variable-reassign> | <const-definition>

nitpick: maybe _external-declaration_ sounds better since definitions are
not statements?

> +
> +(* Variables *)
> +<variable-definition> ::= 'var' <ws> <variable-name> <ows> ':' <ows> <type> <ows> <variable-assign>?
> +<const-definition>    ::= 'const' <ws> <variable-name> <ows> ':' <ows> <type> <ows> <variable-assign>

We don't need to define twice the variable-definition for "var" and
"const", let's combine both in a single rule.

> +<variable-name>       ::= <identifier>
> +<variable-assign>     ::= '=' <ows> <expression>
> +<variable-reassign>   ::= <variable-name> <ows> <variable-assign> <end-of-statement>
>  
>  (* Functions *)
>  <function-definition> ::= 'fn' <ws> <function-name> <ows>
> @@ -38,11 +46,11 @@ language.
>  <block>               ::= '{' <ows> <statement> <ows> (<end-of-statement>
>  <ows> <statement> <ows>)* <end-of-statement>? <ows> '}'
>  <end-of-statement>    ::= ';' | <line-break> | <end-of-file>
> -<statement>           ::= <return-statement>
> +<statement>           ::= <return-statement> | <variable-definition> | <variable-reassign> | <const-definition>
>  <return-statement>    ::= 'return' <ws> <expression>
>  
>  (* Expressions *)
> -<expression>          ::= <integer>
> +<expression>          ::= <integer> | <identifier>
>  
>  (* Identifiers *)
>  <type>                ::= 'u32'
> -- 
> 2.34.1
> 

I have done few adjustments according to my feedback (I also fix the
unnecessary line breaks on function and block).  Let me know what you
think about it.

---->8----
Subject: [PATCH olang] fixup! docs: spec: add variables and constants specification


diff --git a/docs/pages/language-specification.md b/docs/pages/language-specification.md
index 708b679..2650dd9 100644
--- a/docs/pages/language-specification.md
+++ b/docs/pages/language-specification.md
@@ -22,35 +22,45 @@ language.
 
 ```
 (* Entry Point *)
-<translation-unit>    ::= <ows> (<global-statements> <end-of-statement>)*
+<translation-unit>     ::= (<ows> <external-declaration> <ows> (<end-of-statement> | <end-of-file>))*
 
-<global-statements>   ::= <function-definition> | <variable-definition> | <variable-reassign> | <const-definition>
+<external-declaration> ::= <function-definition> 
+                         | <variable-definition>
+                         | <assign-expression>
 
 (* Variables *)
-<variable-definition> ::= 'var' <ws> <variable-name> <ows> ':' <ows> <type> <ows> <variable-assign>?
-<const-definition>    ::= 'const' <ws> <variable-name> <ows> ':' <ows> <type> <ows> <variable-assign>
-
-<variable-name>       ::= <identifier>
-<variable-assign>     ::= '=' <ows> <expression>
-<variable-reassign>   ::= <variable-name> <ows> <variable-assign> <end-of-statement>
+<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> '}'
-<end-of-statement>    ::= ';' | <line-break> | <end-of-file>
-<statement>           ::= <return-statement> | <variable-definition> | <variable-reassign> | <const-definition>
+<block>               ::= '{' <ows> <statement> <ows> (<end-of-statement> <ows> <statement> <ows>)* <end-of-statement>? <ows> '}'
+<end-of-statement>    ::= ';' | <line-break>
+<statement>           ::= <return-statement> | <variable-definition> | <assign-expression>
 <return-statement>    ::= 'return' <ws> <expression>
 
 (* Expressions *)
-<expression>          ::= <integer> | <identifier>
+<expression>          ::= <integer> | <identifier> | <assign-expression>
+<assign-expression>   ::= <variable-name> <ows> <assign-operator> <ows> <expression>
+<assign-operator>     ::= '='
+                        | '*='
+                        | '/='
+                        | '%='
+                        | '+='
+                        | '-='
+                        | '<<='
+                        | '>>='
+                        | '&='
+                        | '^='
+                        | '|='
 
 (* Identifiers *)
 <type>                ::= 'u32'

  parent reply	other threads:[~2024-03-27 20:36 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-27  3:21 [PATCH olang v1 0/2] docs: variables specification Carlos Maniero
2024-03-27  3:21 ` [PATCH olang v1 1/2] docs: spec: rename program to translation-unit Carlos Maniero
2024-03-27 21:20   ` Johnny Richard
2024-03-28 13:50     ` Carlos Maniero
2024-03-27  3:21 ` [PATCH olang v1 2/2] docs: spec: add variables and constants specification Carlos Maniero
2024-03-27  3:22   ` [olang/patches/.build.yml] build failed builds.sr.ht
2024-03-27 21:37   ` Johnny Richard [this message]
2024-03-28 14:11     ` [PATCH olang v1 2/2] docs: spec: add variables and constants specification Carlos Maniero
2024-04-01 17:48       ` Johnny Richard
2024-03-30  1:14     ` Carlos Maniero
2024-04-01 17:54       ` Johnny Richard
2024-04-11 22:39     ` [PATCH] fixup! " ricardo_kagawa
2024-04-12 22:36       ` Johnny Richard
2024-04-13  2:18         ` Carlos Maniero
2024-04-16 19:01         ` Johnny Richard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=u7zpxqr57xx7ulhvzb35oinkz3ywnvjgq4i5x4b63kx4hvy6qq@7xnpwsr2w7y2 \
    --to=johnny@johnnyrichard.com \
    --cc=carlos@maniero.me \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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