view modules/list.tp @ 251:2557ce4e671f

Fix a couple of compiler bugs. topenv was getting initialized in multiple places. This resulted in multiple copies of modules getting created which caused problems for macro expansion. Additionally, arguments were not being marked as declared during code generation so assigning to an argument that was not closed over generated invalid C code.
author Michael Pavone <pavone@retrodev.com>
date Fri, 11 Apr 2014 22:29:32 -0700
parents f3d5068cbd43
children bb4723fec05e
line wrap: on
line source

{
	_empty <- #{
		length <- { 0 }
		empty? <- { true }
		fold:with <- :acc :fun { acc }
		foldr:with <- :acc :fun { acc }
		map <- :fun { self }
		| <- :val {
			list node: val withTail: self
		}
		. <- :right { right }
		reverse <- { self }
		join <- { "" }
		contains? <- :val { false }
		string <- { "[]" }
		print <- { print: string }
	}
	#{
		empty <- { _empty }
		node:withTail <- :_val :_tail {
			#{
				value <- { _val }
				tail <- { _tail }
				empty? <- { false }
				length <- {
					fold: 0 with: :acc val { acc + 1 }
				}
				fold:with <- :acc :fun {
					cur <- self
					while: { not: (cur empty?)} do: {
						acc <- fun: acc (cur value)
						cur <- cur tail
					}
					acc
				}
				foldr:with <- :acc fun {
					fun: (_tail foldr: acc with: fun) _val
				}
				map <- :fun {
					node: (fun: _val) withTail: (_tail map: fun)
				}
				foreach <- :self fun {
					fold: 0 with: :idx el{
						fun: idx el
						idx + 1
					}
					self
				}
				| <- :val {
					node: val withTail: self
				}
				. <- :right {
					foldr: right with: :tail val {
						node: val withTail: tail
					}
				}
				reverse <- {
					fold: [] with: :tail val {
						node: val withTail: tail
					}
				}
				join <- :sep {
					_tail fold: (string: _val) with: :acc el {
						acc . sep . el
					}
				}
				contains? <- :val {
					if: _val = val {
						true
					} else: {
						_tail contains?: val
					}
				}
				string <- {
					(fold: "[\n" with: :acc el {
						acc . "	" . (string: el) . "\n"
					}) . "]"
				}
				print <- {
					print: "[\n"
					foreach: :_ el {
						print: "	" . (string: el) . "\n"
					}
					print: "]"
				}
			}
		}
	}
}