PageRenderTime 28ms CodeModel.GetById 20ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/tools/dspec/ast.d

http://github.com/wilkie/djehuty
D | 133 lines | 106 code | 21 blank | 6 comment | 7 complexity | 772b10e2d600de6300f84a8dee418241 MD5 | raw file
  1module ast;
  2
  3import djehuty;
  4
  5import io.console;
  6
  7class AST
  8{
  9public:
 10	enum ValueType
 11	{
 12		Unsigned,
 13		Signed,
 14		Object,
 15		Pointer,
 16		String,
 17		Hint,
 18		Name
 19	}
 20	
 21protected:
 22	
 23	AST children[2];
 24
 25	struct values
 26	{
 27		ulong unsigned;
 28		long signed;
 29		Object object;
 30		void* pointer;
 31		string str;
 32		string hint;
 33	}
 34
 35	union valueHolder
 36	{
 37		ValueType type;
 38		values value;
 39	}
 40	
 41	valueHolder data;
 42
 43public:
 44	this(AST left, AST right)
 45	{
 46		children[0] = left;
 47		children[1] = right;
 48	}
 49	
 50	void left(AST left)
 51	{
 52		children[0] = left;
 53	}
 54	
 55	void right(AST right)
 56	{
 57		children[1] = right;
 58	}
 59	
 60	AST left()
 61	{
 62		return children[0];
 63	}
 64	
 65	AST right()
 66	{
 67		return children[1];
 68	}
 69
 70	void value(ulong val)
 71	{
 72		data.type = ValueType.Unsigned;
 73		data.value.unsigned = val;
 74	}
 75
 76	void value(string s)
 77	{
 78		data.type = ValueType.String;
 79		data.value.str = s.dup;
 80	}
 81
 82	void hint(string s)
 83	{
 84		data.type = ValueType.Hint;
 85		data.value.str = s.dup;
 86	}
 87
 88	void name(string s)
 89	{
 90		data.type = ValueType.Name;
 91		data.value.str = s.dup;
 92	}
 93	
 94	void walk(uint depth = 0)
 95	{
 96		static char[] spaces = "`---------------------------------------------------------------------------";
 97		if (data.type == ValueType.Hint)
 98		{
 99			// *** Console.putln(...);
100			Console.putln(spaces[0..depth*2], "HINT: ", data.value.str, " [", left, ", ", right, "]");
101			//writefln(spaces[0..depth*2], "HINT: ", data.value.str.array, " [", left, ", ", right, "]");
102		}
103		else if (data.type == ValueType.Name)
104		{
105			// *** Console.putln(...);
106			Console.putln(spaces[0..depth*2], data.value.str, " [", left, ", ", right, "]");
107			//writefln(spaces[0..depth*2], data.value.str.array, " [", left, ", ", right, "]");
108		}
109		else
110		{
111			// *** Console.putln(...);
112			Console.putln(spaces[0..depth*2], "\"", data.value.str.replace('\n', ' '), "\" [", left, ", ", right, "]");
113			//writefln(spaces[0..depth*2], "\"", replace(data.value.str.array, "\n", " "), "\" [", left, ", ", right, "]");
114		}
115		if (left !is null) { left.walk(depth+1); }
116		if (right !is null) { right.walk(depth+1); }
117	}
118
119	ValueType valueType()
120	{
121		return data.type;
122	}
123	
124	void getValue(out string value)
125	{
126		value = data.value.str.dup;
127	}
128	
129	void getValue(out ulong value)
130	{
131		value = data.value.unsigned;
132	}
133}