[vala/wip/baedert/nullable: 7/25] add AstPrinter
- From: Timm Bäder <baedert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/wip/baedert/nullable: 7/25] add AstPrinter
- Date: Tue, 8 Nov 2016 21:18:38 +0000 (UTC)
commit 8cf80826c994ebec8f7e0bf2e259899ad23e7318
Author: Timm Bäder <mail baedert org>
Date: Sat Nov 5 20:32:36 2016 +0100
add AstPrinter
compiler/valacompiler.vala | 7 +
vala/Makefile.am | 1 +
vala/valaastprinter.vala | 346 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 354 insertions(+), 0 deletions(-)
---
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala
index 38564fc..9dcd9bc 100644
--- a/compiler/valacompiler.vala
+++ b/compiler/valacompiler.vala
@@ -89,6 +89,7 @@ class Vala.Compiler {
static bool disable_version_header;
static bool fatal_warnings;
static bool disable_diagnostic_colors;
+ static bool print_ast;
static string dependencies;
static string entry_point;
@@ -152,6 +153,7 @@ class Vala.Compiler {
{ "gresources", 0, 0, OptionArg.STRING_ARRAY, ref gresources, "XML of gresources", "FILE..."
},
{ "enable-version-header", 0, 0, OptionArg.NONE, ref enable_version_header, "Write vala build
version in generated files", null },
{ "disable-version-header", 0, 0, OptionArg.NONE, ref disable_version_header, "Do not write
vala build version in generated files", null },
+ { "print-ast", 0, 0, OptionArg.NONE, ref print_ast, "Print AST", null },
{ "", 0, 0, OptionArg.FILENAME_ARRAY, ref sources, null, "FILE..." },
{ null }
};
@@ -341,6 +343,11 @@ class Vala.Compiler {
var parser = new Parser ();
parser.parse (context);
+ if (print_ast) {
+ var printer = new AstPrinter ();
+ printer.print_ast (context);
+ }
+
var genie_parser = new Genie.Parser ();
genie_parser.parse (context);
diff --git a/vala/Makefile.am b/vala/Makefile.am
index 80bc3ea..8b13a2f 100644
--- a/vala/Makefile.am
+++ b/vala/Makefile.am
@@ -25,6 +25,7 @@ libvalacore_la_VALASOURCES = \
valaarrayresizemethod.vala \
valaarraytype.vala \
valaassignment.vala \
+ valaastprinter.vala \
valaattribute.vala \
valabaseaccess.vala \
valabasicblock.vala \
diff --git a/vala/valaastprinter.vala b/vala/valaastprinter.vala
new file mode 100644
index 0000000..a2767ee
--- /dev/null
+++ b/vala/valaastprinter.vala
@@ -0,0 +1,346 @@
+
+public class Vala.AstPrinter : CodeVisitor {
+ private CodeContext context;
+ private int level = 0;
+
+ private void print (string msg) {
+ for (int i = 0; i < level; i ++)
+ stdout.printf (" ");
+
+ stdout.printf ("%s\n", msg);
+ }
+
+ public AstPrinter () {
+ }
+
+ public void print_ast (CodeContext context) {
+ this.context = context;
+ context.accept (this);
+ }
+
+
+ public override void visit_namespace (Namespace ns) {
+ print ("Namespace %s".printf (ns.name));
+
+ level ++;
+ ns.accept_children (this);
+ level --;
+ }
+
+ public override void visit_method (Method m) {
+ print ("Method %s".printf (m.name));
+ m.accept_children (this);
+ }
+
+ public override void visit_source_file (SourceFile source_file) {
+ }
+ public override void visit_class (Class cl) {
+ }
+ public override void visit_struct (Struct st) {
+ }
+
+ public override void visit_interface (Interface iface) {}
+
+ public override void visit_enum (Enum en) {
+ }
+ public override void visit_enum_value (EnumValue ev) {
+ }
+
+ public override void visit_error_domain (ErrorDomain edomain) {
+ }
+ public override void visit_error_code (ErrorCode ecode) {
+ }
+
+ public override void visit_constant (Constant c) {
+ }
+ public override void visit_field (Field f) {
+ }
+
+ public override void visit_creation_method (CreationMethod m) {
+ }
+
+ public override void visit_formal_parameter (Parameter p) {
+ }
+
+ public override void visit_property (Property prop) {
+ }
+
+
+ public override void visit_property_accessor (PropertyAccessor acc) {
+ }
+
+ public override void visit_signal (Signal sig) {
+ }
+
+
+ public override void visit_constructor (Constructor c) {
+ }
+
+ public override void visit_destructor (Destructor d) {
+ }
+
+ public override void visit_type_parameter (TypeParameter p) {
+ }
+
+ public override void visit_using_directive (UsingDirective ns) {
+ }
+
+ public override void visit_data_type (DataType type) {
+ }
+
+
+ public override void visit_block (Block b) {
+ level ++;
+ b.accept_children (this);
+ level --;
+ }
+
+
+ public override void visit_empty_statement (EmptyStatement stmt) {
+ }
+
+
+ public override void visit_declaration_statement (DeclarationStatement stmt) {
+ DataType? symbol_type = context.analyzer.get_value_type_for_symbol (stmt.declaration, false);
+
+ string type = symbol_type != null ? symbol_type.to_qualified_string () : "??";
+ print ("Variable Declaration: %s %s".printf (type, stmt.declaration.name));
+ }
+
+
+ public override void visit_local_variable (LocalVariable local) {
+ print ("Local Var %s %s".printf (local.variable_type.to_string (), local.name));
+ }
+
+ public override void visit_initializer_list (InitializerList list) {
+ }
+
+
+ public override void visit_expression_statement (ExpressionStatement stmt) {
+ print ("ExpressionStatement");
+ level ++;
+ stmt.accept_children (this);
+ level --;
+ }
+
+ public override void visit_if_statement (IfStatement stmt) {
+ print ("If");
+ level ++;
+ stmt.condition.accept (this);
+ level --;
+
+ print ("True Block");
+ level ++;
+ stmt.true_statement.accept (this);
+ level --;
+ if (stmt.false_statement != null) {
+ print ("False Block");
+ level ++;
+ stmt.false_statement.accept (this);
+ level --;
+ }
+ }
+
+
+ public override void visit_switch_statement (SwitchStatement stmt) {
+ }
+
+ public override void visit_switch_section (SwitchSection section) {
+ }
+
+
+ public override void visit_switch_label (SwitchLabel label) {
+ }
+
+ public override void visit_loop (Loop stmt) {
+ }
+
+ public override void visit_while_statement (WhileStatement stmt) {
+ }
+
+ public override void visit_do_statement (DoStatement stmt) {
+ }
+
+
+ public override void visit_for_statement (ForStatement stmt) {
+ }
+
+
+ public override void visit_foreach_statement (ForeachStatement stmt) {
+ }
+
+
+ public override void visit_break_statement (BreakStatement stmt) {
+ }
+
+
+ public override void visit_continue_statement (ContinueStatement stmt) {
+ }
+
+ public override void visit_return_statement (ReturnStatement stmt) {
+ }
+
+
+ public override void visit_yield_statement (YieldStatement y) {
+ }
+
+
+ public override void visit_throw_statement (ThrowStatement stmt) {
+ }
+
+
+ public override void visit_try_statement (TryStatement stmt) {
+ }
+
+
+ public override void visit_catch_clause (CatchClause clause) {
+ }
+
+
+ public override void visit_lock_statement (LockStatement stmt) {
+ }
+
+ public override void visit_unlock_statement (UnlockStatement stmt) {
+ }
+
+ public override void visit_delete_statement (DeleteStatement stmt) {
+ }
+
+
+ private string type_str (DataType? t) {
+ if (t == null) return "??";
+ else return t.to_string ();
+ }
+ public override void visit_expression (Expression expr) {
+ //print ("Expression %s (value_type: %s, target_type: %s, constant: %s, non_null: %s)"
+ //.printf (expr.type_name, type_str (expr.value_type), type_str (expr.target_type),
+ //expr.is_constant ().to_string (), expr.is_non_null
().to_string ()));
+ }
+
+
+ public override void visit_array_creation_expression (ArrayCreationExpression expr) {
+ }
+
+
+ public override void visit_boolean_literal (BooleanLiteral lit) {
+ }
+
+
+ public override void visit_character_literal (CharacterLiteral lit) {
+ }
+
+
+ public override void visit_integer_literal (IntegerLiteral lit) {
+ }
+
+ public override void visit_real_literal (RealLiteral lit) {
+ }
+
+ public override void visit_regex_literal (RegexLiteral lit) {
+ }
+
+
+
+ public override void visit_string_literal (StringLiteral lit) {
+ }
+
+
+ public override void visit_template (Template tmpl) {
+ }
+
+ public override void visit_tuple (Tuple tuple) {
+ }
+
+
+ public override void visit_null_literal (NullLiteral lit) {
+ }
+
+
+ public override void visit_member_access (MemberAccess expr) {
+ print ("MemberAccess (%s)".printf (expr.member_name));
+ level ++;
+ expr.accept_children (this);
+ level --;
+ }
+
+
+ public override void visit_method_call (MethodCall expr) {
+ }
+
+
+ public override void visit_element_access (ElementAccess expr) {
+ }
+
+
+ public override void visit_slice_expression (SliceExpression expr) {
+ }
+
+ public override void visit_base_access (BaseAccess expr) {
+ }
+
+
+ public override void visit_postfix_expression (PostfixExpression expr) {
+ }
+
+
+ public override void visit_object_creation_expression (ObjectCreationExpression expr) {
+ }
+
+
+ public override void visit_sizeof_expression (SizeofExpression expr) {
+ }
+
+
+ public override void visit_typeof_expression (TypeofExpression expr) {
+ }
+
+
+ public override void visit_unary_expression (UnaryExpression expr) {
+ }
+
+ public override void visit_cast_expression (CastExpression expr) {
+ }
+
+ public override void visit_named_argument (NamedArgument expr) {
+ }
+
+
+ public override void visit_pointer_indirection (PointerIndirection expr) {
+ }
+
+
+ public override void visit_addressof_expression (AddressofExpression expr) {
+ }
+
+ public override void visit_reference_transfer_expression (ReferenceTransferExpression expr) {
+ }
+
+ public override void visit_binary_expression (BinaryExpression expr) {
+ }
+
+ public override void visit_type_check (TypeCheck expr) {
+ }
+
+ public override void visit_conditional_expression (ConditionalExpression expr) {
+ }
+
+ public override void visit_lambda_expression (LambdaExpression expr) {
+ }
+
+ public override void visit_assignment (Assignment a) {
+ print ("From");
+ level ++;
+ a.left.accept (this);
+ level --;
+
+ print ("To");
+ level ++;
+ a.right.accept (this);
+ level --;
+ //print ("Assignment from %s to %s".printf (a.left.type_name, a.right.type_name));
+ }
+
+ public override void visit_end_full_expression (Expression expr) {
+ }
+
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]