Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3 Extended: New Types #189

Merged
merged 45 commits into from
Jan 17, 2024
Merged

V3 Extended: New Types #189

merged 45 commits into from
Jan 17, 2024

Conversation

rturrado
Copy link
Contributor

@rturrado rturrado commented Jan 9, 2024

Add axis, bool, int, float, bool array, int array, and float array definitions and initializations.

Initialization lists are treated independently, i.e., without considering to what they will be assigned.
Initialization lists parsing yields a constant array of a given type.
In order to determine this type, we walk the list, and keep track of the highest type to which we can promote.
For example, for a list containing the types bool and int, that type would be int.
An initialization list can only contain elements of type bool, int, or float.

For a variable declaration including an initialization:

  • We first check the right hand side, resolving any identifier.
    This allows us to avoid the case of declaring a variable and assigning itself as a value.
    E.g., int i = i.
  • We then register the left hand side.
  • And, finally, we perform the assignment.
    Assignments of arrays first consider the length of the arrays.
    Assignments consider promotions.
    A bool value can be promoted to int or float.
    An int value can be promoted to float.
    A bool array value can be promoted to int array or float array.
    An int array value can be promoted to int array or float array.
    A bool array, int array, or float array of 3 elements can be promoted to an axis type.

Changes to v1x files

  • res/v1:
    • Remove directories with names containing dashes. These directories were renamed in a previous commit to names using underscores. But some of the old directories remained there.
    • Golden files using variable definitions have changed because we no longer allow multiple variable definitions per line.

Added axis, bool, float and int keywords to CqasmLexer.g4.
- Added OPEN_BRACE, CLOSE_BRACE.
- INTEGER_LITERAL and FLOAT_LITERAL can be negative.

Parser:
- Added type definitions for axis, bool, bool array, int, int array, float, and float array.

BuildTreeGenAstVisitor:
- Added new methods with an empty implementation.
…cores.

These folders have already been renamed to others containing dashes.
…, int, int array, float, and float array type definitions.
Updated res/v3x/parsing/expression_list/no_second_part test.
Because we have changed the grammar files, and that changes the error messages.
- axisTypeInitializer rule is now an expression called axisInitializationList.
- arrayTypeInitializer rule is now an expression called initializationList.
Added axis, bool, bool_array, int_array, and real_array types to cqasm-types.
Fixed some warnings in src/v1x/cqasm-types.cpp and src/{v1x,v3x}/cqasm-values.cpp.
Changed visitStatement method so that it can receive more than one statement.
For a variable definition with initialization, a visitor method will return two statements, the variable declaration and the assignment.
Added an assignment instruction to cqasm-ast.tree.
Added an assignment instruction to cqasm-semantic.tree.

TODO:
1) The statement 'int i = i' is passing the tests at the moment. It is maybe a matter of processing the assignment instruction before the variable declaration.
2) Initialization lists are not yet implemented.
An initialization is a variable declaration plus an assignment instruction.

The analyzer:
- first resolves the right-hand side operand, i.e., the value to which we want to initialize the variable,
- then declares the variable, and,
- finally, does the 'var = rhs' assignment.
This way, the statement 'int i = i' does not pass the semantic checks.
Because the right-hand side operand cannot be resolved.

cqasm-ast.tree:
- Added 'initialization' statement.
- Changed 'variables' to 'variable'. We only allow single variable declarations for the time being.

CqasmParser.g4:
- Changed Definition to Declaration.
- A declaration will be a definition or an initialization (declaration plus assignment).
- 'Variables.names: []' is now 'Variable.name: <>'.
- Variables plus AssignmentInstruction is now Initialization.
Added TODOs for AnalyzeTreeGenAstVisitor::visitAssignmentOperands.
AnalyzeTreeGenAstVisitor.cpp: reimplemented visitInitialization.
BuildTreeGenAstVisitor.cpp: fixed visitInitializationList.
cqasm-ast.tree: added initialization_list expressions.
cqasm-types.tree: all types have a size now.
cqasm-values.tree:  added const_int_array.
cqasm-values.cpp: added range_of(value) to get the number of elements of a given value.

TODO: I think visitInititalization may be simplified a lot.
TODO: some tests are now failing because all types have a size now.
TODO: test 'int[3] i = { -5, 0, 5 }' fails because it tries to promote a lhs of type int array to a rhs of type int.
- All types have a size now.
- Implemented initializations for int type declarations.
…ization} files.

Add bbit_b, qqubit_q, iint i test folders.
Rename '3_14' to '3.14' in test folders.
Update values::promote function to promote booleans to integer, real, and complex.
BuildTreeGenAstVisitor::{syntaxError,setNodeAnnotation} are now const.
BuildTreeGenAstVisitor::setNodeAnnotation receives now a 'const One<Node>&'.
CustomErrorListener::CustomErrorListener receives now a std::string.
Add ConstBoolArray and ConstRealArray values.
Add check_promote function to check if a value can be promoted to a type.
…iles.

Rename '3_14' to '3.14' in test folders.
…itialization}.

cqasm-values: 'promote' method considers now array types, and variable values.
CqasmLexer.g4: INTEGER_LITERALs and FLOAT_LITERALs cannot be negative. To achieve this, we will have to implement the unary minus operator.
- doAssignment changes slightly the error message.
- index references of size 1 return a simple type and not an array type.

Add res/v3x/parsing/{assignment,axis_{definition,initialization},initialization_list} files.
AnalyzeTreeGenAstVisitor:
- Factor some code out into doAssignment.
- visitInitialization list throws if list is empty.
- TODO: reimplement visitInitializationList so that expressions_highest_type is set to the type of the first expression.

cqasm-values:
- Add element_type_of function.
- Change type_of function so that index references of size 1 return a simple type and not an array type. E.g., type_of q[1] returns Qubit, and not QubitArray.
An axis variable is considered to always have size 3.

For cQasm 1.x, axis type was implemented as an enum taking 3 possible values: X, Y, or Z.
For cQasm 3.0, axis type is implemented as an array of 3 real values, each corresponding to vectors on the X, Y, and Z axes.

Boolean, integer, and real arrays promote to axis (an array of 3 real values).
@rturrado rturrado requested a review from pablolh January 9, 2024 10:46
Rename values::promoteArrayValueToArrayType to promote_array_value_to_array_type.
Move function templates from cqasm-values.cpp to cqasm-values.hpp.
Add 'std::' for uint32_t types, and include cstdint header.
Remove an unnecessary 'tree::' in BuildTreeGenAstVisitor.cpp.
# Conflicts:
#	.github/workflows/test.yml
#	include/v3x/AnalyzeTreeGenAstVisitor.hpp
#	include/v3x/BuildTreeGenAstVisitor.hpp
#	res/v1x/parsing/grammar/expression_recovery/ast.golden.txt
#	res/v1x/parsing/grammar/expression_recovery/input.cq
#	res/v1x/parsing/grammar/index_negative/ast.golden.txt
#	res/v1x/parsing/grammar/index_negative/input.cq
#	res/v1x/parsing/grammar/index_positive/ast.golden.txt
#	res/v1x/parsing/grammar/index_positive/input.cq
#	res/v1x/parsing/grammar/index_positive/semantic.1.1.golden.txt
#	res/v1x/parsing/grammar/index_positive/semantic.1.2.golden.txt
#	res/v1x/parsing/grammar/subcircuit_default/ast.golden.txt
#	res/v1x/parsing/grammar/subcircuit_default/input.cq
#	res/v1x/parsing/grammar/subcircuit_default/semantic.1.0.golden.txt
#	res/v1x/parsing/grammar/subcircuit_default/semantic.1.1.golden.txt
#	res/v1x/parsing/grammar/subcircuit_default/semantic.1.2.golden.txt
#	res/v1x/parsing/grammar/subcircuit_no_default/ast.golden.txt
#	res/v1x/parsing/grammar/subcircuit_no_default/input.cq
#	res/v1x/parsing/grammar/subcircuit_no_default/semantic.1.0.golden.txt
#	res/v1x/parsing/grammar/subcircuit_no_default/semantic.1.1.golden.txt
#	res/v1x/parsing/grammar/subcircuit_no_default/semantic.1.2.golden.txt
#	res/v3x/parsing/measure_instruction/bit_and_qubit/semantic.3.0.golden.txt
#	res/v3x/parsing/measure_instruction/q0_to_b_0/semantic.3.0.golden.txt
#	res/v3x/parsing/measure_instruction/q_0_to_b0/semantic.3.0.golden.txt
#	src/v3x/AnalyzeTreeGenAstVisitor.cpp
#	src/v3x/cqasm-ast.tree
res/v3x/parsing/axis_definition/axis_a/ast.golden.txt Outdated Show resolved Hide resolved
src/v1x/cqasm-primitives.cpp Show resolved Hide resolved
src/v3x/AnalyzeTreeGenAstVisitor.cpp Outdated Show resolved Hide resolved
src/v3x/AnalyzeTreeGenAstVisitor.cpp Show resolved Hide resolved
src/v3x/BuildTreeGenAstVisitor.cpp Show resolved Hide resolved
src/v3x/BuildTreeGenAstVisitor.cpp Outdated Show resolved Hide resolved
src/v3x/BuildTreeGenAstVisitor.cpp Outdated Show resolved Hide resolved
src/v3x/cqasm-values.cpp Show resolved Hide resolved
src/v3x/cqasm-values.cpp Outdated Show resolved Hide resolved
…14').

Added some missing golden files.
Updated some test files. Because:
- Variables is now Variable.
- All types have a size now.
- AnalyzeTreeGenAstVisitor.cpp: exception handling code that pushes an error back but doesn't throw now resets the return value.
- BuildTreeGenAstVisitor.cpp: token is not declared as a reference anymore.
- cqasm-ast.tree: we now have identifiers and keywords (both string primitives). Type names are keywords.
- cqasm-primitives: Axis type is now a class containing x, y, and z doubles.
- cqasm-values.cpp: check_promote uses if-else instead of ternary operator.

I've changed the semantics of an initialization list so that it only allows having constant bool, int, or real elements.
…d::any>.

There is even more boilerplate code now.
I think this is due to the fact that visit methods use references as parameters.
That forbids polymorphism of nodes to kick in.
Instead, as_<node_type> has to be used to visit the appropriate nodes.

Also, callers to visitor methods have to std::any_cast the return values, because visitor methods return std::any.

On top of that, some methods return an empty std::any.

On the plus side, visit_index_list, visit_index_item, and visit_index_range do not need the extra size argument.
The out of range checks are done at visit_index.

visit_index_list has been fixed to not discard previous indices when traversing an index range.

Some throw statements have been fixed to not include an AST node.
This is not needed if the throw statement is caught later and a context added to the error.
Copy link
Contributor

@pablolh pablolh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice
I'll make a second one for the visitor commit, reviewing separately

src/v3x/AnalyzeTreeGenAstVisitor.cpp Outdated Show resolved Hide resolved
src/v3x/cqasm-primitives.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@pablolh pablolh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments whose content we mostly already discussed

src/v3x/AnalyzeTreeGenAstVisitor.cpp Outdated Show resolved Hide resolved
src/v3x/AnalyzeTreeGenAstVisitor.cpp Show resolved Hide resolved
src/v3x/AnalyzeTreeGenAstVisitor.cpp Outdated Show resolved Hide resolved
src/v3x/AnalyzeTreeGenAstVisitor.cpp Show resolved Hide resolved
src/v3x/AnalyzeTreeGenAstVisitor.cpp Outdated Show resolved Hide resolved
We do not consider axis comparisons for the time being.

Use the default implementation for the Axis equality operator.
Rename values::range_of to values::size_of.
Add types::size_of.
Add visit_boolean_literal, visit_integer_literal, visit_float_literal, and visit_identifier.
Add visit_expression to factorize some common code for the different types of expressions.
Copy link
Contributor

@pablolh pablolh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me now! :)

@rturrado
Copy link
Contributor Author

Great, thanks!

@rturrado rturrado merged commit fc632fa into develop Jan 17, 2024
17 checks passed
@rturrado rturrado deleted the v3-extended-new_types branch January 18, 2024 11:42
@rturrado rturrado mentioned this pull request Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants