-
Notifications
You must be signed in to change notification settings - Fork 16
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
Conversation
Added axis, bool, float and int keywords to CqasmLexer.g4.
…cores. These folders have already been renamed to others containing dashes.
…, int, int array, float, and float array type definitions.
…lization,int_array_initialization} tests.
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.
…izationList has been reimplemented.
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).
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
…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.
There was a problem hiding this 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
There was a problem hiding this 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
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.
There was a problem hiding this 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! :)
Great, thanks! |
Add
axis
,bool
,int
,float
,bool
array,int
array, andfloat
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
andint
, that type would beint
.An initialization list can only contain elements of type
bool
,int
, orfloat
.For a variable declaration including an initialization:
This allows us to avoid the case of declaring a variable and assigning itself as a value.
E.g.,
int i = i
.Assignments of arrays first consider the length of the arrays.
Assignments consider promotions.
A
bool
value can be promoted toint
orfloat
.An
int
value can be promoted tofloat
.A
bool
array value can be promoted toint
array orfloat
array.An
int
array value can be promoted toint
array orfloat
array.A
bool
array,int
array, orfloat
array of 3 elements can be promoted to anaxis
type.Changes to
v1x
filesres/v1
: