view modules/ast.tp @ 246:8c81afd6d2d3

Refactor some of the AST node object constructors into a separate AST module
author Michael Pavone <pavone@retrodev.com>
date Mon, 06 Jan 2014 19:41:35 -0800
parents
children b76f683d076e
line wrap: on
line source
{
	_binary <- 0
	_string <- 1
	_int    <- 2

	#{
		binary <- { _binary }
		stringlit <- { _string }
		intlit <- { _int }


		binaryOp:withArgs <- :opname :_left _right {
			#{
				nodeType <- { _binary }
				left <- _left
				op <- opname
				right <- _right

				leftAssociative? <- {
					op != "|"
				}
				stringIndent <- :indent {
					(left stringIndent: indent) . " " . op . (right stringIndent: indent)
				}
				string <- {
					stringIndent: ""
				}
			}
		}

		stringLit <- :_val {
			#{
				nodeType <- { _string }
				val <- _val
				stringIndent <- :indent {
					"\"" . val . "\""
				}
				string <- {
					stringIndent: ""
				}
			}
		}

		intLit:withBits:andBase:signed? <- :_val :_bits :_base :_signed? {
			#{
				nodeType <- { _int }
				val <- _val
				base <- _base
				bits <- _bits
				signed? <- _signed?
				stringIndent <- :indent {
					suffix <- ""
					if: bits != 32 || (not: signed?) {
						suffix <- (if: signed? {"i"} else: {"u"}) . bits
					}
					if: base = 16 {
						"0x" . (hex: val) . suffix
					} else: {
						if: base = 2 {
							str <- "0b"
							i <- bits - 1
							printzero <- false
							while: { i >= 0 } do: {
								str <- str . (if: (lshift: 1 by: i) and val > 0 {
									printzero <- true
									"1"
								} else: {
									if: printzero {"0"} else: {""}
								})
								i <- i - 1
							}
							str . suffix
						} else: {
							(string: val) . suffix
						}
					}
				}
				string <- {
					stringIndent: ""
				}
			}
		}
	}
}