parent
f993d49856
commit
1908561d31
|
@ -146,6 +146,19 @@ public class CodeGeneratorVisitor : TypeCheckVisitor
|
||||||
Builder.EndScope();
|
Builder.EndScope();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void PreVisit(Factor factor)
|
||||||
|
{
|
||||||
|
base.PreVisit(factor);
|
||||||
|
|
||||||
|
factor.OnParethnesisGenerator += (_, e) => { e.Expression.IsCondition = factor.IsCondition; };
|
||||||
|
|
||||||
|
factor.OnNotGenerator += (_, e) => { e.Factor.IsCondition = factor.IsCondition; };
|
||||||
|
|
||||||
|
factor.OnUminusGenerator += (_, e) => { e.Factor.IsCondition = factor.IsCondition; };
|
||||||
|
|
||||||
|
factor.OnPlusGenerator += (_, e) => { e.Factor.IsCondition = factor.IsCondition; };
|
||||||
|
}
|
||||||
|
|
||||||
public override void PostVisit(Factor factor)
|
public override void PostVisit(Factor factor)
|
||||||
{
|
{
|
||||||
base.PostVisit(factor);
|
base.PostVisit(factor);
|
||||||
|
@ -256,7 +269,23 @@ public class CodeGeneratorVisitor : TypeCheckVisitor
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
variable.VariableName = variable.Identifier.IdentifierName;
|
// 虽然这里这样可能会生成来类似于 &*x 的代码
|
||||||
|
// 但是可以正常运行
|
||||||
|
variable.VariableName =
|
||||||
|
symbol.Reference ? $"*{variable.Identifier.IdentifierName}" : variable.Identifier.IdentifierName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void PreVisit(Term term)
|
||||||
|
{
|
||||||
|
base.PreVisit(term);
|
||||||
|
|
||||||
|
term.OnMultiplyGenerator += (_, e) =>
|
||||||
|
{
|
||||||
|
e.Left.IsCondition = term.IsCondition;
|
||||||
|
e.Right.IsCondition = term.IsCondition;
|
||||||
|
};
|
||||||
|
|
||||||
|
term.OnFactorGenerator += (_, e) => { e.Factor.IsCondition = term.IsCondition; };
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void PostVisit(Term term)
|
public override void PostVisit(Term term)
|
||||||
|
@ -276,6 +305,19 @@ public class CodeGeneratorVisitor : TypeCheckVisitor
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void PreVisit(SimpleExpression simpleExpression)
|
||||||
|
{
|
||||||
|
base.PreVisit(simpleExpression);
|
||||||
|
|
||||||
|
simpleExpression.OnAddGenerator += (_, e) =>
|
||||||
|
{
|
||||||
|
e.Left.IsCondition = simpleExpression.IsCondition;
|
||||||
|
e.Right.IsCondition = simpleExpression.IsCondition;
|
||||||
|
};
|
||||||
|
|
||||||
|
simpleExpression.OnTermGenerator += (_, e) => { e.Term.IsCondition = simpleExpression.IsCondition; };
|
||||||
|
}
|
||||||
|
|
||||||
public override void PostVisit(SimpleExpression simpleExpression)
|
public override void PostVisit(SimpleExpression simpleExpression)
|
||||||
{
|
{
|
||||||
base.PostVisit(simpleExpression);
|
base.PostVisit(simpleExpression);
|
||||||
|
@ -298,6 +340,16 @@ public class CodeGeneratorVisitor : TypeCheckVisitor
|
||||||
{
|
{
|
||||||
base.PreVisit(expression);
|
base.PreVisit(expression);
|
||||||
|
|
||||||
|
expression.OnSimpleExpressionGenerator += (_, e) =>
|
||||||
|
{
|
||||||
|
e.SimpleExpression.IsCondition = expression.IsCondition;
|
||||||
|
};
|
||||||
|
|
||||||
|
expression.OnRelationGenerator += (_, e) =>
|
||||||
|
{
|
||||||
|
e.Left.IsCondition = expression.IsCondition;
|
||||||
|
e.Right.IsCondition = expression.IsCondition;
|
||||||
|
};
|
||||||
if (expression.IsWhileCondition)
|
if (expression.IsWhileCondition)
|
||||||
{
|
{
|
||||||
GenerateWhileLabel();
|
GenerateWhileLabel();
|
||||||
|
@ -306,6 +358,7 @@ public class CodeGeneratorVisitor : TypeCheckVisitor
|
||||||
""");
|
""");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void PostVisit(Expression expression)
|
public override void PostVisit(Expression expression)
|
||||||
{
|
{
|
||||||
base.PostVisit(expression);
|
base.PostVisit(expression);
|
||||||
|
@ -422,7 +475,11 @@ public class CodeGeneratorVisitor : TypeCheckVisitor
|
||||||
{
|
{
|
||||||
base.PreVisit(statement);
|
base.PreVisit(statement);
|
||||||
|
|
||||||
statement.OnIfGenerator += (_, e) => { e.Condition.IsIfCondition = true; };
|
statement.OnIfGenerator += (_, e) =>
|
||||||
|
{
|
||||||
|
e.Condition.IsIfCondition = true;
|
||||||
|
e.Condition.IsCondition = true;
|
||||||
|
};
|
||||||
|
|
||||||
statement.OnForGenerator += (_, e) =>
|
statement.OnForGenerator += (_, e) =>
|
||||||
{
|
{
|
||||||
|
@ -445,16 +502,6 @@ public class CodeGeneratorVisitor : TypeCheckVisitor
|
||||||
|
|
||||||
statement.OnAssignGenerator += (_, e) =>
|
statement.OnAssignGenerator += (_, e) =>
|
||||||
{
|
{
|
||||||
if (!SymbolTable.TryGetSymbol(e.Variable.Identifier.IdentifierName, out Symbol? symbol))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (symbol.Reference)
|
|
||||||
{
|
|
||||||
e.Variable.VariableName = "*" + e.Variable.VariableName;
|
|
||||||
}
|
|
||||||
|
|
||||||
Builder.AddLine($"{e.Variable.VariableName} = {e.Expression.VariableName};");
|
Builder.AddLine($"{e.Variable.VariableName} = {e.Expression.VariableName};");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -686,7 +733,7 @@ public class CodeGeneratorVisitor : TypeCheckVisitor
|
||||||
|
|
||||||
if (procedureId == "write" || procedureId == "writeln")
|
if (procedureId == "write" || procedureId == "writeln")
|
||||||
{
|
{
|
||||||
string result = $"printf(\"{GenerateFormatString(parameters) + isReturn}\"";
|
string result = $"printf(\"{GenerateFormatString(parameters, true) + isReturn}\"";
|
||||||
|
|
||||||
foreach (Expression parameter in parameters)
|
foreach (Expression parameter in parameters)
|
||||||
{
|
{
|
||||||
|
@ -748,7 +795,22 @@ public class CodeGeneratorVisitor : TypeCheckVisitor
|
||||||
{
|
{
|
||||||
if (parameterType.IsVar)
|
if (parameterType.IsVar)
|
||||||
{
|
{
|
||||||
parameterValue += $", &{parameter.VariableName}";
|
// 这里需要判断parameter是否也为引用类型
|
||||||
|
if (SymbolTable.TryGetSymbol(parameter.VariableName, out Symbol? parameterSymboe))
|
||||||
|
{
|
||||||
|
if (parameterSymboe.Reference)
|
||||||
|
{
|
||||||
|
parameterValue += $", {parameter.VariableName}";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
parameterValue += $", &{parameter.VariableName}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
parameterValue += $", &{parameter.VariableName}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -768,22 +830,23 @@ public class CodeGeneratorVisitor : TypeCheckVisitor
|
||||||
{
|
{
|
||||||
if (expression.VariableType == PascalBasicType.Integer)
|
if (expression.VariableType == PascalBasicType.Integer)
|
||||||
{
|
{
|
||||||
value += "%d ";
|
value += "%d";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expression.VariableType == PascalBasicType.Real)
|
if (expression.VariableType == PascalBasicType.Real)
|
||||||
{
|
{
|
||||||
// 这里需要按照输出调整
|
// 这里需要按照输出调整
|
||||||
value += output ? "%.6lf " : "%lf ";
|
// 在输出real的前面需要添加一个空格
|
||||||
|
value += output ? "%.6lf" : "%lf";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expression.VariableType == PascalBasicType.Character)
|
if (expression.VariableType == PascalBasicType.Character)
|
||||||
{
|
{
|
||||||
value += "%c ";
|
value += "%c";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return value.Trim();
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GenerateBasicTypeString(PascalType pascalType)
|
private static string GenerateBasicTypeString(PascalType pascalType)
|
||||||
|
|
|
@ -48,6 +48,11 @@ public class Expression : NonTerminatedSyntaxNode
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsIfCondition { get; set; }
|
public bool IsIfCondition { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否为条件判断语句
|
||||||
|
/// </summary>
|
||||||
|
public bool IsCondition { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否为WHILE语句中的条件语句
|
/// 是否为WHILE语句中的条件语句
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -50,6 +50,11 @@ public class Factor : NonTerminatedSyntaxNode
|
||||||
{
|
{
|
||||||
public override NonTerminatorType Type => NonTerminatorType.Factor;
|
public override NonTerminatorType Type => NonTerminatorType.Factor;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否为条件判断语句
|
||||||
|
/// </summary>
|
||||||
|
public bool IsCondition { get; set; }
|
||||||
|
|
||||||
public override void PreVisit(SyntaxNodeVisitor visitor)
|
public override void PreVisit(SyntaxNodeVisitor visitor)
|
||||||
{
|
{
|
||||||
visitor.PreVisit(this);
|
visitor.PreVisit(this);
|
||||||
|
@ -158,10 +163,11 @@ public class Factor : NonTerminatedSyntaxNode
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// factor -> id ( )
|
// factor -> id ( )
|
||||||
OnProcedureCallGenerator?.Invoke(this, new ProcedureCallGeneratorEventArgs
|
OnProcedureCallGenerator?.Invoke(this,
|
||||||
{
|
new ProcedureCallGeneratorEventArgs
|
||||||
ProcedureName = terminatedSyntaxNode.Token.Convert<IdentifierSemanticToken>()
|
{
|
||||||
});
|
ProcedureName = terminatedSyntaxNode.Token.Convert<IdentifierSemanticToken>()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (Children.Count == 4)
|
else if (Children.Count == 4)
|
||||||
|
@ -177,7 +183,6 @@ public class Factor : NonTerminatedSyntaxNode
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
SemanticToken token = Children[0].Convert<TerminatedSyntaxNode>().Token;
|
SemanticToken token = Children[0].Convert<TerminatedSyntaxNode>().Token;
|
||||||
Factor factor = Children[1].Convert<Factor>();
|
Factor factor = Children[1].Convert<Factor>();
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,11 @@ public class SimpleExpression : NonTerminatedSyntaxNode
|
||||||
{
|
{
|
||||||
public override NonTerminatorType Type => NonTerminatorType.SimpleExpression;
|
public override NonTerminatorType Type => NonTerminatorType.SimpleExpression;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否为条件判断语句
|
||||||
|
/// </summary>
|
||||||
|
public bool IsCondition { get; set; }
|
||||||
|
|
||||||
public override void PreVisit(SyntaxNodeVisitor visitor)
|
public override void PreVisit(SyntaxNodeVisitor visitor)
|
||||||
{
|
{
|
||||||
visitor.PreVisit(this);
|
visitor.PreVisit(this);
|
||||||
|
@ -52,19 +57,17 @@ public class SimpleExpression : NonTerminatedSyntaxNode
|
||||||
{
|
{
|
||||||
if (Children.Count == 1)
|
if (Children.Count == 1)
|
||||||
{
|
{
|
||||||
OnTermGenerator?.Invoke(this, new TermGeneratorEventArgs
|
OnTermGenerator?.Invoke(this, new TermGeneratorEventArgs { Term = Children[0].Convert<Term>() });
|
||||||
{
|
|
||||||
Term = Children[0].Convert<Term>()
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
OnAddGenerator?.Invoke(this, new AddGeneratorEventArgs
|
OnAddGenerator?.Invoke(this,
|
||||||
{
|
new AddGeneratorEventArgs
|
||||||
Left = Children[0].Convert<SimpleExpression>(),
|
{
|
||||||
Operator = Children[1].Convert<AddOperator>(),
|
Left = Children[0].Convert<SimpleExpression>(),
|
||||||
Right = Children[2].Convert<Term>()
|
Operator = Children[1].Convert<AddOperator>(),
|
||||||
});
|
Right = Children[2].Convert<Term>()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
OnTermGenerator = null;
|
OnTermGenerator = null;
|
||||||
|
|
|
@ -21,6 +21,11 @@ public class Term : NonTerminatedSyntaxNode
|
||||||
{
|
{
|
||||||
public override NonTerminatorType Type => NonTerminatorType.Term;
|
public override NonTerminatorType Type => NonTerminatorType.Term;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否为条件判断语句
|
||||||
|
/// </summary>
|
||||||
|
public bool IsCondition { get; set; }
|
||||||
|
|
||||||
public override void PreVisit(SyntaxNodeVisitor visitor)
|
public override void PreVisit(SyntaxNodeVisitor visitor)
|
||||||
{
|
{
|
||||||
visitor.PreVisit(this);
|
visitor.PreVisit(this);
|
||||||
|
@ -52,19 +57,17 @@ public class Term : NonTerminatedSyntaxNode
|
||||||
{
|
{
|
||||||
if (Children.Count == 1)
|
if (Children.Count == 1)
|
||||||
{
|
{
|
||||||
OnFactorGenerator?.Invoke(this, new FactorGeneratorEventArgs
|
OnFactorGenerator?.Invoke(this, new FactorGeneratorEventArgs { Factor = Children[0].Convert<Factor>() });
|
||||||
{
|
|
||||||
Factor = Children[0].Convert<Factor>()
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
OnMultiplyGenerator?.Invoke(this, new MultiplyGeneratorEventArgs
|
OnMultiplyGenerator?.Invoke(this,
|
||||||
{
|
new MultiplyGeneratorEventArgs
|
||||||
Left = Children[0].Convert<Term>(),
|
{
|
||||||
Operator = Children[1].Convert<MultiplyOperator>(),
|
Left = Children[0].Convert<Term>(),
|
||||||
Right = Children[2].Convert<Factor>()
|
Operator = Children[1].Convert<MultiplyOperator>(),
|
||||||
});
|
Right = Children[2].Convert<Factor>()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
OnFactorGenerator = null;
|
OnFactorGenerator = null;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user