Struct semantic_analyzer::semantic::State
source · pub struct State {
pub global: GlobalState,
pub context: Vec<Rc<RefCell<BlockState>>>,
pub errors: Vec<StateErrorResult>,
}Expand description
Fields§
§global: GlobalState§context: Vec<Rc<RefCell<BlockState>>>§errors: Vec<StateErrorResult>Implementations§
source§impl State
impl State
sourcepub const fn import(&self, _data: &ImportPath<'_>)
pub const fn import(&self, _data: &ImportPath<'_>)
Import analyzer
sourcepub fn types(&mut self, data: &StructTypes<'_>)
pub fn types(&mut self, data: &StructTypes<'_>)
Types declaration analyzer. Add types to Global State. Currently only one type kind: Structs
sourcepub fn function_declaration(&mut self, data: &FunctionStatement<'_>)
pub fn function_declaration(&mut self, data: &FunctionStatement<'_>)
Function declaration analyze. Add it to Global State/
sourcepub fn function_body(&mut self, data: &FunctionStatement<'_>)
pub fn function_body(&mut self, data: &FunctionStatement<'_>)
Function body analyze. It is basic execution entity for program flow. It’s operate sub analyze for function elements. It’s contain Body State for current and child states.
sourcepub fn let_binding(
&mut self,
data: &LetBinding<'_>,
function_state: &Rc<RefCell<BlockState>>
)
pub fn let_binding( &mut self, data: &LetBinding<'_>, function_state: &Rc<RefCell<BlockState>> )
Let-binding statement
Analyze let-binding statement:
- Let value bind from expression. First should be analysed
expressionfor binding value. - Generate value for current state. Special field
inner_namethat used as name forCodegenshould be unique in current state and for all parent states. For thatinner_namethe inner value name counter incremented. - Set
Valueparameters:inner_name, type and allocation status - Insert value to current values state map: value
name->Data - Store
inner_namein current and parent states - Codegen
sourcepub fn binding(
&mut self,
data: &Binding<'_>,
function_state: &Rc<RefCell<BlockState>>
)
pub fn binding( &mut self, data: &Binding<'_>, function_state: &Rc<RefCell<BlockState>> )
Binding statement
Analyze binding statement for mutable variables:
- Bind from expression. First should be analysed
expressionfor binding value. - Read value for current state.
- Update value to current values state map: value
name->Data - Codegen with Store action
sourcepub fn function_call(
&mut self,
data: &FunctionCall<'_>,
body_state: &Rc<RefCell<BlockState>>
) -> Option<Type>
pub fn function_call( &mut self, data: &FunctionCall<'_>, body_state: &Rc<RefCell<BlockState>> ) -> Option<Type>
Function-call
Call function with function parameters arguments. Arguments is expressions.
- Check is current function name exists in global state of functions name.
- Analyse expressions for function parameters
- Inc register
- Generate codegen Codegen store always result to register even for void result.
Errors
Return error if function name doesn’t exist in global state
sourcepub fn condition_expression(
&mut self,
data: &ExpressionLogicCondition<'_>,
function_body_state: &Rc<RefCell<BlockState>>
)
pub fn condition_expression( &mut self, data: &ExpressionLogicCondition<'_>, function_body_state: &Rc<RefCell<BlockState>> )
condition-expression
Analyse condition operations.
sourcepub fn if_condition_body(
&mut self,
body: &[IfBodyStatement<'_>],
if_body_state: &Rc<RefCell<BlockState>>
)
pub fn if_condition_body( &mut self, body: &[IfBodyStatement<'_>], if_body_state: &Rc<RefCell<BlockState>> )
sourcepub fn if_condition_loop_body(
&mut self,
body: &[IfLoopBodyStatement<'_>],
if_body_state: &Rc<RefCell<BlockState>>,
label_loop_start: &LabelName,
label_loop_end: &LabelName
)
pub fn if_condition_loop_body( &mut self, body: &[IfLoopBodyStatement<'_>], if_body_state: &Rc<RefCell<BlockState>>, label_loop_start: &LabelName, label_loop_end: &LabelName )
sourcepub fn if_condition_calculation(
&mut self,
condition: &IfCondition<'_>,
if_body_state: &Rc<RefCell<BlockState>>,
label_if_begin: &LabelName,
label_if_else: &LabelName,
label_if_end: &LabelName,
is_else: bool
)
pub fn if_condition_calculation( &mut self, condition: &IfCondition<'_>, if_body_state: &Rc<RefCell<BlockState>>, label_if_begin: &LabelName, label_if_else: &LabelName, label_if_end: &LabelName, is_else: bool )
If conditions calculations
Calculate conditions for if-condition. It can contain simple and logic conditions.
sourcepub fn if_condition(
&mut self,
data: &IfStatement<'_>,
function_body_state: &Rc<RefCell<BlockState>>,
label_end: Option<LabelName>,
label_loop: Option<(&LabelName, &LabelName)>
)
pub fn if_condition( &mut self, data: &IfStatement<'_>, function_body_state: &Rc<RefCell<BlockState>>, label_end: Option<LabelName>, label_loop: Option<(&LabelName, &LabelName)> )
If-condition
Analyzing includes all variants for if statements:
- if
- if-else
- if-else-if
It creates own state, with parent function-state. in that case
if-state independent from parent state, but csn get access to
parent state.
If condition can’t contain
elseandif-elseon the same time.
Special case for label_end - it should be set from previous
context, and main goal is to end all of if-condition nodes in
the same flow with same if-end label. It’s especially important
for else-if condition.
sourcepub fn loop_statement(
&mut self,
data: &[LoopBodyStatement<'_>],
function_body_state: &Rc<RefCell<BlockState>>
)
pub fn loop_statement( &mut self, data: &[LoopBodyStatement<'_>], function_body_state: &Rc<RefCell<BlockState>> )
sourcepub fn expression(
&mut self,
data: &Expression<'_>,
body_state: &Rc<RefCell<BlockState>>
) -> Option<ExpressionResult>
pub fn expression( &mut self, data: &Expression<'_>, body_state: &Rc<RefCell<BlockState>> ) -> Option<ExpressionResult>
Expression
Is basic entity for state operation and state usage. State correctness verified by expressions call. Expressions folded by operations priority. For that expressions tree folded each leaf of tree by priority operation level. The most striking image is bracketing an expression with a higher priority, and build tree based on that.
Return
PrimitiveValue | TmpRegister
Possible algorithm conditions: 1. PrimitiveValue -> PrimitiveValue 2. Value -> load -> TmpRegister 3. FuncCall -> call -> TmpRegister 4. Operations 4.1. PrimitiveValue - PrimitiveValue -> tmp = OP val1, val2 -> TmpRegister - Value -> tmp1 = load -> OP val1, tmp1 -> TmpRegister - FuncCAll -> tmp1 = call -> OP val1, tmp1 -> TmpRegister 4.2. TmpRegister (with name tmp1) - PrimitiveValue -> tmp2 = OP tmp1, val1 -> TmpRegister - Value -> tmp2 = load -> tmp3 = OP tmp1, tmp2 -> TmpRegister - FuncCall -> tmp2 = call -> tmp3 = OP tmp1, tmp2 -> TmpRegister 4.3. Operations -> recursively invoke 4.2.
sourcepub fn expression_operation(
&mut self,
left_value: Option<&ExpressionResult>,
right_expression: &Expression<'_>,
op: Option<&ExpressionOperations>,
body_state: &Rc<RefCell<BlockState>>
) -> Option<ExpressionResult>
pub fn expression_operation( &mut self, left_value: Option<&ExpressionResult>, right_expression: &Expression<'_>, op: Option<&ExpressionOperations>, body_state: &Rc<RefCell<BlockState>> ) -> Option<ExpressionResult>
Expression operation semantic logic:
OP(lhs, rhs)
Left-value contains optional Exrpression result for left side
of expression.