Skip to content

Direct IR parsing#64

Open
Doomsk wants to merge 40 commits intodev/pythonfrom
dev/python_impl/ir_parsing
Open

Direct IR parsing#64
Doomsk wants to merge 40 commits intodev/pythonfrom
dev/python_impl/ir_parsing

Conversation

@Doomsk
Copy link
Copy Markdown
Member

@Doomsk Doomsk commented Jun 29, 2025

Objective

Testing a direct IR parsing approach (text -> IR -> execution), instead of AST parsing (text -> AST -> IR -> execution).

Points

Advantages

  • It cuts through some double checks and double work from transforming text to AST and then from AST to IR
  • Converts the text directly to the object that will be executed, making the interpreter faster
  • Some code logics and structures can be checked during the parsing process, preventing further computation for faulty code

Disadvantages

  • There are some complex checks and operations that need to be done during parsing
  • Some checks can only be made after the whole code is parsed, making some double checks happening anyways (partially though)

Exploration

IR was initially built as a monolithic code. But caveats emerged from it, namely:

  1. If different files have the same imports but using different labels, the code would break
  2. If different files have different imports but with the same name (e.g. importing type plane from euclidian in file a.hat and importing type plane from non-euclidian in file b.hat, and both files being imported on main.hat), the code would break

A solution: independently parse and keep IR separated, making them related to the appropriate others, in the form of an IR graph:

ir_graph

Advantages

  • Easier to parse
  • Easier to maintain
  • Easier to reason about relationships
  • Code could be easily updatable in one IR
  • Distributed computation-prepared

Disadvantages

  • Updating code may become complex with the size of the source code and the internal relationship between files
  • Not clear when to propagate a file update during evaluation/execution time
  • Not clear how an error (on parsing or something else) should be shown or whether it will break the running code or just the new parsed one

Doomsk and others added 17 commits May 13, 2025 23:02
* new start to match main and this branch

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more documentation

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more documentation

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix mkdocs ci

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more doc pages

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add TODOs, docs introductions, mike versioning, codes fix here and there

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more docs

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix small typo

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

---------

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
* new start to match main and this branch

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more documentation

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more documentation

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix mkdocs ci

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more doc pages

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add TODOs, docs introductions, mike versioning, codes fix here and there

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more docs

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix small typo

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add docs build from main branch

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

---------

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
* new start to match main and this branch

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more documentation

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more documentation

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix mkdocs ci

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more doc pages

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add TODOs, docs introductions, mike versioning, codes fix here and there

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more docs

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix small typo

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add docs build from main branch

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more docs descriptions, pre-commit config
improve heap memory logic and Heather dialect grammar (in progress)

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

---------

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
* new start to match main and this branch

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more documentation

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more documentation

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix mkdocs ci

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more doc pages

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add TODOs, docs introductions, mike versioning, codes fix here and there

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more docs

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix small typo

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add docs build from main branch

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more docs descriptions, pre-commit config
improve heap memory logic and Heather dialect grammar (in progress)

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add instructions docs
improve docs overall

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

---------

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
* add fresh new structure

* add memblock and tests

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more code

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* improve memory block logic and structure

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* improve test

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* include data and type container

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* include data and type container

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* new start to match main and this branch

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more documentation

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more documentation

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix mkdocs ci

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more doc pages

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add TODOs, docs introductions, mike versioning, codes fix here and there

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more docs

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix small typo

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add docs build from main branch

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add more docs descriptions, pre-commit config
improve heap memory logic and Heather dialect grammar (in progress)

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add instructions docs
improve docs overall

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add logo and favicon

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* restart some rust codebase

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* restart some rust codebase

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix pre-commit

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add github actions workflows for lint and tests

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix pre-commit and pytest

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix pre-commit and pytest

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix pre-commit location

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* remove netqasm for now

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix pre-commit and pytest actions

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix pre-commit action

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix pytest action

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix pytest action

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix working directory for github actions

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* add qiskit-aer dependency

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

---------

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* fix ast code
add Heather AST parsing visitor

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* small fix

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

* small fix

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>

---------

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
* Add QNez instruction and update LowLeveQLang for OpenQASM v2.0 support

* reformat for pre-commit

* fix mypy exit error

* Add QInstrFlag to manage instruction behavior and update QNez for argument generation

* Add skip_gen flag to conditionally generate arguments in gen_instrs method

* QNez: Update boolean handling for mask values in instruction processing

* Update argument in test for QNez instruction from '@redim' to '@Not'
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
# Conflicts:
#	python/src/hhat_lang/low_level/quantum_lang/openqasm/v2/qlang.py
#	python/tests/lowlevel/qlang/openqasm/v2/test_lowlevelqlang.py
add visitor ir
test visitor ir

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
@Doomsk Doomsk self-assigned this Jun 29, 2025
@Doomsk Doomsk added python Pull requests that update Python code experimental labels Jun 29, 2025
Doomsk and others added 5 commits July 6, 2025 22:37
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
implement IR base classes to inherit from hhat_lang.core

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
…parser and evaluator

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
add function importer
improve variables, memory stack, heap, and symbol tables
finish IR parsing visitor
improve grammar
fix create new file on project

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
@Doomsk
Copy link
Copy Markdown
Member Author

Doomsk commented Jul 14, 2025

Successful example code

each file included:

// file: src/main.hat

use (
  type:[
    geometry.{euclidian2.{line plane} differential2.normal}
    std.io.socket
  ]
  fn:math.{sin floor}
)

main {
  l:line =.{x=41}  // declare and assign
  p:plane
  p.{x=250 y=600}
  print(sin(0.0))
}
// file: src/math.hat

fn floor (x:f64) i64 {
  xi:i64 = x*i64
  ::if(and(ltz(x) ne(x xi*f64)):sub(xi 1) true:xi)
}

fn mod-2pi (theta:f64) f64 {
  two-pi:f64 = 6.283185307179586
  quot:i64 = floor(div(theta two-pi))
  ::sub(theta mul(two-pi quot*f64))
}

fn mod-pi (theta:f64) f64 {
  pi:f64 = 3.141592653589793
  two-pi:f64 = 6.283185307179586
  quot:i64 = floor(div(add(theta pi) two-pi))
  ::sub(theta mul(two-pi quot*f64))
}

fn abs (x:f64) f64 {
  bit63:u64 = 9223372036854775807 // sub(pow(2 63) 1), clear sign bit
  b:u64
  memcpy(b<ref> x<ref> sizeof(b))
  memcpy(x<ref> b-and(b bit63)<ref> sizeof(x))
  ::x
}

fn sin (theta:f64) f64 {
  pi:f64 = 3.141592653589793
  pi2:f64 = pow(pi 2.0)
  new-theta:f64 = mod-pi(theta)
  abs-theta:f64 = if(ltz(new-theta):neg(new-theta) true:new-theta)
  quad-approx:f64 = sub(div(4.0 pi) div(mul(4.0 abs(new-theta)) pi2))
  ::mul(new-theta quad-approx)
}
// file: src/hat_types/geometry/euclidian2.hat

type point:i64
type line {x:i32}
type surface:u64
type plane {x:i32 y:i32}
// file: src/hat_types/geometry/differential2.hat

type normal {dx:i32 dy:i32 dz:i32}
// file: src/hat_types/std/io.hat

type socket {raw:u32}

Final result as IR parsed code

[ir/start]
  types:
      line<struct>{x:i32}
      plane<struct>{x:i32 y:i32}
      normal<struct>{dx:i32 dy:i32 dz:i32}
      socket<struct>{raw:u32}

  fns:
      fn(name=floor, args=(f64)):
         FN-DEF#:NAME#[floor] ARGS#[ARG-VALUE#[x:f64]] TYPE#[i64]
            IRFlag.DECLARE_ASSIGN(xi, i64, IRFlag.CAST(x, i64))
            RETURN#[IRFlag.CALL_WITH_OPTION(if, OPTION#[IRFlag.CALL(sub, xi 1:int):IRFlag.CALL(and, IRFlag.CALL(ltz, x) IRFlag.CALL(ne, x IRFlag.CAST(xi, f64)))])]

      fn(name=mod-2pi, args=(f64)):
         FN-DEF#:NAME#[mod-2pi] ARGS#[ARG-VALUE#[theta:f64]] TYPE#[f64]
            IRFlag.DECLARE_ASSIGN(two-pi, f64, 6.283185307179586:float)
            IRFlag.DECLARE_ASSIGN(quot, i64, IRFlag.CALL(floor, IRFlag.CALL(div, theta two-pi)))
            RETURN#[IRFlag.CALL(sub, theta IRFlag.CALL(mul, two-pi IRFlag.CAST(quot, f64)))]

      fn(name=mod-pi, args=(f64)):
         FN-DEF#:NAME#[mod-pi] ARGS#[ARG-VALUE#[theta:f64]] TYPE#[f64]
            IRFlag.DECLARE_ASSIGN(pi, f64, 3.141592653589793:float)
            IRFlag.DECLARE_ASSIGN(two-pi, f64, 6.283185307179586:float)
            IRFlag.DECLARE_ASSIGN(quot, i64, IRFlag.CALL(floor, IRFlag.CALL(div, IRFlag.CALL(add, theta pi) two-pi)))
            RETURN#[IRFlag.CALL(sub, theta IRFlag.CALL(mul, two-pi IRFlag.CAST(quot, f64)))]

      fn(name=abs, args=(f64)):
         FN-DEF#:NAME#[abs] ARGS#[ARG-VALUE#[x:f64]] TYPE#[f64]
            IRFlag.DECLARE_ASSIGN(bit63, u64, 9223372036854775807:int)
            IRFlag.DECLARE(b, u64)
            IRFlag.CALL(memcpy, b<ref> x<ref> IRFlag.CALL(sizeof, b))
            IRFlag.CALL(memcpy, x<ref> IRFlag.CALL(b-and, b bit63)<ref> IRFlag.CALL(sizeof, x))
            RETURN#[x]

      fn(name=sin, args=(f64)):
         FN-DEF#:NAME#[sin] ARGS#[ARG-VALUE#[theta:f64]] TYPE#[f64]
            IRFlag.DECLARE_ASSIGN(pi, f64, 3.141592653589793:float)
            IRFlag.DECLARE_ASSIGN(pi2, f64, IRFlag.CALL(pow, pi 2.0:float))
            IRFlag.DECLARE_ASSIGN(new-theta, f64, IRFlag.CALL(mod-pi, theta))
            IRFlag.DECLARE_ASSIGN(abs-theta, f64, IRFlag.CALL_WITH_OPTION(if, OPTION#[IRFlag.CALL(neg, new-theta):IRFlag.CALL(ltz, new-theta)]))
            IRFlag.DECLARE_ASSIGN(quad-approx, f64, IRFlag.CALL(sub, IRFlag.CALL(div, 4.0:float pi) IRFlag.CALL(div, IRFlag.CALL(mul, 4.0:float IRFlag.CALL(abs, new-theta)) pi2)))
            RETURN#[IRFlag.CALL(mul, new-theta quad-approx)]

  main:
       IRFlag.DECLARE_ASSIGN(l, line, ARG-VALUE#[x:41:int])
       IRFlag.DECLARE(p, plane)
       IRFlag.ASSIGN(p, ARG-VALUE#[x:250:int] ARG-VALUE#[y:600:int])
       IRFlag.CALL(print, IRFlag.CALL(sin, 0.0:float))

[ir/end]

A few things to improve, but so far it looks much better!

improve IR logic with symbol tables and ref tables
add enum data structure
(wip) improve IR visitor

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
@Doomsk
Copy link
Copy Markdown
Member Author

Doomsk commented Jul 24, 2025

Rough structure for memory and IR so far

IR_diagrams_light

There are a few substantial changes that improve code structure, access and logic, but I will comment later, when the IR is tested.

Doomsk added 2 commits August 7, 2025 18:05
(wip) improve IR visitor

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
…nd functions

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
Doomsk added 8 commits August 13, 2025 01:00
improve other codes to utilize IR graph logic
(wip) implement IR visitor with IR graph

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
add PEG grammar in python for faster parsing
fix type and function imports, ref tables
fix IR visitor

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
(wip) big cleanup, big refactor

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
big cleanup, big refactor

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
@Doomsk Doomsk marked this pull request as ready for review August 27, 2025 08:09
@Doomsk Doomsk requested a review from q-inho August 27, 2025 08:09
@Doomsk Doomsk changed the base branch from main to dev/python September 25, 2025 17:13
# Conflicts:
#	.github/workflows/ci.yml
#	README.md
#	docs/CNAME
#	docs/TODOs.md
#	docs/core/index.md
#	docs/dialects/creation.md
#	docs/dialects/heather/current_syntax.md
#	docs/dialects/heather/heather_syntax.md
#	docs/dialects/heather/index.md
#	docs/index.md
#	docs/python/python_guide.md
#	docs/running_hhat.md
#	docs/stylesheets/extra.css
#	docs/toolchain.md
#	mkdocs.yml
#	python/README.md
#	python/pyproject.toml
#	python/src/hhat_lang/core/code/instructions.py
#	python/src/hhat_lang/core/code/utils.py
#	python/src/hhat_lang/core/data/core.py
#	python/src/hhat_lang/core/data/utils.py
#	python/src/hhat_lang/core/data/variable.py
#	python/src/hhat_lang/core/error_handlers/errors.py
#	python/src/hhat_lang/core/execution/abstract_base.py
#	python/src/hhat_lang/core/execution/abstract_program.py
#	python/src/hhat_lang/core/lowlevel/abstract_qlang.py
#	python/src/hhat_lang/core/memory/core.py
#	python/src/hhat_lang/core/types/__init__.py
#	python/src/hhat_lang/core/types/abstract_base.py
#	python/src/hhat_lang/core/types/core.py
#	python/src/hhat_lang/core/types/resolve_sizes.py
#	python/src/hhat_lang/core/utils.py
#	python/src/hhat_lang/dialects/heather/grammar/grammar.peg
#	python/src/hhat_lang/low_level/quantum_lang/openqasm/v2/__init__.py
#	python/src/hhat_lang/low_level/quantum_lang/openqasm/v2/instructions.py
#	python/src/hhat_lang/low_level/quantum_lang/openqasm/v2/qlang.py
#	python/src/hhat_lang/low_level/target_backend/qiskit/openqasm/code_executor.py
#	python/src/hhat_lang/toolchain/project/new.py
#	python/tests/core/test_type_ds.py
#	python/tests/dialects/heather/parsing/ex_fn01.hat
#	python/tests/dialects/heather/parsing/ex_fn02.hat
#	python/tests/dialects/heather/parsing/ex_main02.hat
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
fix Heather's quantum program.py

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
fix ci workflow

Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimental python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants