diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-07-19 22:59:29 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-07-19 22:59:37 -0700 |
| commit | 9f0b5384d792aae309645875f5c138d36c7c60a9 (patch) | |
| tree | 195259656b922bf43abb3be472b2c39ecf9c72cd /nomsu_compiler.moon | |
| parent | 53bec8091f019b939861ed94730111785283e163 (diff) | |
Improving and cleaning up nomsu codegen.
Diffstat (limited to 'nomsu_compiler.moon')
| -rw-r--r-- | nomsu_compiler.moon | 98 |
1 files changed, 43 insertions, 55 deletions
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon index d856a83..f2c3ec0 100644 --- a/nomsu_compiler.moon +++ b/nomsu_compiler.moon @@ -601,8 +601,7 @@ with NomsuCompiler unless visited[c] insert(comments, c) visited[c] = true - for x in *t - find_comments(x) if AST.is_syntax_tree x + find_comments(x) for x in *t when AST.is_syntax_tree x find_comments(tree) -- Sort in reversed order so they can be easily popped table.sort(comments, (a,b)->(a.pos > b.pos)) @@ -610,14 +609,13 @@ with NomsuCompiler pop_comments = (pos, prefix='', suffix='')-> nomsu = NomsuCode(tree.source) for i=#comments,1,-1 - comment = comments[i] - break if comment.pos > pos - comments[i] = nil - nomsu\append("#"..(gsub(comment.comment, "\n", "\n ")).."\n") - if comment.comment\match("^\n.") then nomsu\append("\n") -- for aesthetics + break if comments[i].pos > pos + comment, comments[i] = comments[i].comment, nil + nomsu\append("#"..(gsub(comment, "\n", "\n ")).."\n") + if comment\match("^\n.") then nomsu\append("\n") -- for aesthetics return '' if #nomsu.bits == 0 - nomsu\prepend prefix - nomsu\append suffix + nomsu\prepend(prefix) unless prefix == '' + nomsu\append(suffix) unless suffix == '' return nomsu -- For concision: @@ -642,58 +640,48 @@ with NomsuCompiler return nomsu when "Action" - pos = tree.source.start + pos, next_space = tree.source.start, '' nomsu = NomsuCode(tree.source, pop_comments(pos)) - next_space = "" for i,bit in ipairs tree - if match(next_space, '\n') - nomsu\append pop_comments(pos, '\n') + if next_space == "\n.." or (next_space == " " and nomsu\trailing_line_len! > MAX_LINE) + nomsu\append "\n", pop_comments(pos), '..' + next_space = "" + if type(bit) == "string" - if next_space == ' ' and (type(tree[i-1]) == 'string' and Parser.is_operator(bit) != Parser.is_operator(tree[i-1])) - next_space = '' - nomsu\append next_space, bit - next_space = " " + unless type(tree[i-1]) == 'string' and Parser.is_operator(tree[i-1]) != Parser.is_operator(bit) + nomsu\append(next_space) + nomsu\append bit + next_space = ' ' else - arg_nomsu = if bit.type == "Block" and #bit > 1 then nil - else @tree_to_inline_nomsu(bit) - if bit.type == "Text" and tostring(arg_nomsu) != '"\\n"' and tostring(arg_nomsu)\match("\\n") - arg_nomsu = nil -- Force long text for multi-line text - next_space = match(next_space, "[^ ]*") if bit.type == "Block" - nomsu\append next_space - if arg_nomsu and nomsu\trailing_line_len! + #tostring(arg_nomsu) < MAX_LINE - if bit.type == "Block" - nomsu\append arg_nomsu - next_space = "\n.." + inline = if bit.type == "Block" and #bit > 1 then nil + else + _inline = @tree_to_inline_nomsu(bit) + (nomsu\trailing_line_len! + #tostring(_inline) < MAX_LINE) and _inline or nil + if bit.type == "Block" + nomsu\append(inline or recurse(bit)) + elseif bit.type == "Action" + nomsu\append next_space + if inline + nomsu\append "(", inline, ")" else - arg_nomsu\parenthesize! if bit.type == "Action" - nomsu\append arg_nomsu - next_space = " " + nomsu\append NomsuCode(bit.source, "(..)\n ", pop_comments(bit.source.start), recurse(bit)) else - arg_nomsu = assert recurse(bit) - -- These types carry their own indentation - switch bit.type - when "List", "Dict", "Text", "Block", "EscapedNomsu" - nomsu\append arg_nomsu - else - nomsu\append NomsuCode(bit.source, "(..)\n ", pop_comments(bit.source.start), arg_nomsu) - next_space = "\n.." + nomsu\append next_space, (inline or recurse(bit)) pos = bit.source.stop + next_space = inline and " " or "\n.." - if next_space == " " and nomsu\trailing_line_len! > MAX_LINE - next_space = "\n.." - nomsu\append pop_comments(pos, '\n') + nomsu\append pop_comments(tree.source.stop, '\n') return nomsu when "EscapedNomsu" inline_nomsu = @tree_to_inline_nomsu tree - if #tostring(inline_nomsu) <= MAX_LINE - return inline_nomsu - nomsu = recurse(tree[1]) - switch tree[1].type + return inline_nomsu unless #tostring(inline_nomsu) > MAX_LINE + return switch tree[1].type when "List", "Dict", "Text", "Block" - return NomsuCode tree.source, "\\", nomsu + NomsuCode tree.source, "\\", recurse(tree[1]) else - return NomsuCode tree.source, "\\(..)\n ", pop_comments(tree.source.start), nomsu + NomsuCode tree.source, "\\(..)\n ", + pop_comments(tree.source.start), recurse(tree[1]) when "Block" nomsu = NomsuCode(tree.source, pop_comments(tree.source.start)) @@ -740,14 +728,13 @@ with NomsuCompiler else interp_nomsu = @tree_to_inline_nomsu(bit) if nomsu\trailing_line_len! + #tostring(interp_nomsu) <= MAX_LINE - switch bit.type - when "Var" - if type(tree[i+1]) == 'string' and not match(tree[i+1], "^[ \n\t,.:;#(){}[%]]") - interp_nomsu\parenthesize! - when "List", "Dict" - nomsu\append "\\", interp_nomsu - else - nomsu\append "\\(", interp_nomsu, ")" + if bit.type == "Var" + if type(tree[i+1]) == 'string' and not match(tree[i+1], "^[ \n\t,.:;#(){}[%]]") + interp_nomsu\parenthesize! + elseif bit.type == "List" or bit.type == "Dict" + nomsu\append "\\", interp_nomsu + else + nomsu\append "\\(", interp_nomsu, ")" else interp_nomsu = recurse(bit) if bit.type != "List" and bit.type != "Dict" and bit.type != "Text" and bit.type != "Block" @@ -766,6 +753,7 @@ with NomsuCompiler assert #tree > 0 nomsu = NomsuCode(tree.source, pop_comments(tree[1].source.start)) for i, item in ipairs tree + inline = @tree_to_inline_nomsu(item) item_nomsu = @tree_to_inline_nomsu(item) item_nomsu\parenthesize! if item.type == "Block" if nomsu\trailing_line_len! + #tostring(item_nomsu) <= MAX_LINE |
