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 2/5] checker: scope: populate symbol's type
Date: Sat, 21 Sep 2024 20:47:45 +0200	[thread overview]
Message-ID: <yp4wjlsbosy4qjmlwundl6zudonhp5sbe3jc4y6khztvpych5a@ttmfupdsc4qf> (raw)
In-Reply-To: <20240921082437.396691-3-carlos@maniero.me>

On Sat, Sep 21, 2024 at 08:24:56AM GMT, Carlos Maniero wrote:
> Persisting the symbol type will be very convenient for codegen once this
> information can be used to determine how many bytes that symbol
> requires.

I also agree.  I would change a little bit here and make the checker
calculate the size in bytes and add it to the symbol.

> diff --git a/src/checker.c b/src/checker.c
> index 3b713f7..f5068e0 100644
> --- a/src/checker.c
> +++ b/src/checker.c
> @@ -23,6 +23,9 @@
>  static void
>  populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast);
>  
> +static void
> +populate_types(checker_t *checker, ast_node_t *ast);
> +
>  checker_t *
>  checker_new(arena_t *arena)
>  {
> @@ -44,6 +47,7 @@ checker_check(checker_t *checker, ast_node_t *ast)
>      assert(ast);
>  
>      scope_t *scope = scope_new(checker->arena);
> +    populate_types(checker, ast);
>      populate_scope(checker, scope, ast);

We are traversing the tree twice to populate_scope.  I think the
populate scope can set the type on scope at this moment.

>  
>      // TODO: traverse the ast tree to verify semantics
> @@ -107,7 +111,8 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
>  
>          case AST_NODE_VAR_DEF: {
>              string_view_t id = ast->as_var_def.identifier;
> -            symbol_t *symbol = symbol_new(checker->arena, id);
> +            type_t type = ast->as_var_def.type;
> +            symbol_t *symbol = symbol_new(checker->arena, id, type);

Let's include the size in bytes for the symbol. I believe this will
enhance our symbol and make it easier to verify in the next steps

>  
>              scope_insert(scope, symbol);
>              ast->as_var_def.scope = scope;
> @@ -124,3 +129,79 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
>              return;
>      }
>  }
> +
> +static void
> +evaluate_type(type_t *type)
> +{
> +    if (string_view_eq_to_cstr(type->id, "u32")) {
> +        type->kind = TYPE_PRIMITIVE;
> +        type->as_primitive = TYPE_U32;
> +        return;
> +    }
> +
> +    type->kind = TYPE_USER_DEFINED;
> +}
> +
> +static void
> +populate_types(checker_t *checker, ast_node_t *ast)
> +{

If we set extra data to symbol's type we don't need to traverse the ast
again here.

Could you please enlighten the motivation behind setting extra data to
types on AST nodes?

> diff --git a/src/type.h b/src/type.h
> new file mode 100644
> index 0000000..855cd83
> --- /dev/null
> +++ b/src/type.h
> @@ -0,0 +1,39 @@
> +/*
> + * Copyright (C) 2024 olang maintainers
> + *
> + * This program is free software: you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation, either version 3 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program.  If not, see <https://www.gnu.org/licenses/>.
> + */
> +#ifndef TYPE_H
> +#define TYPE_H
> +typedef enum
> +{
> +    TYPE_PRIMITIVE,
> +    TYPE_USER_DEFINED
> +} type_kind_t;

I don't see other kinds in the future.  What do you think about adding a
boolean property named "is_primitive"?

  reply	other threads:[~2024-09-21 18:47 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-21  8:24 [PATCH olang 0/5] extend unsined integers types (u8, u16, u64) Carlos Maniero
2024-09-21  8:24 ` [PATCH olang 1/5] parser: replace type enum to an struction string id representation Carlos Maniero
2024-09-21 17:59   ` Johnny Richard
2024-09-21  8:24 ` [PATCH olang 2/5] checker: scope: populate symbol's type Carlos Maniero
2024-09-21 18:47   ` Johnny Richard [this message]
2024-09-21 21:23     ` Carlos Maniero
2024-09-22 13:46       ` Johnny Richard
2024-09-21  8:24 ` [PATCH olang 3/5] codegen: fix map simbol list type Carlos Maniero
2024-09-21 18:50   ` Johnny Richard
2024-09-21  8:25 ` [PATCH olang 4/5] codegen: calculate the variable offset based on symbol type Carlos Maniero
2024-09-21 18:56   ` Johnny Richard
2024-09-21  8:25 ` [PATCH olang 5/5] codegen: perform mov instructions based on variable type Carlos Maniero
2024-09-21  8:26   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-21 19:17   ` [PATCH olang 5/5] codegen: perform mov instructions based on variable type Johnny Richard
2024-09-21 21:30     ` Carlos Maniero
2024-09-22 14:16 ` [PATCH olang 0/5] extend unsined integers types (u8, u16, u64) 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=yp4wjlsbosy4qjmlwundl6zudonhp5sbe3jc4y6khztvpych5a@ttmfupdsc4qf \
    --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