Compiler/code generator: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (Reverted edits by Jwells1213 (talk) to last revision by Chemoelectric) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 34:
|-
| style="vertical-align:top" |
<
while (count < 10) {
print("count is: ", count, "\n");
count = count + 1;
}</
| style="vertical-align:top" |
Line 136:
Loading this data into an internal parse tree should be as simple as:
<
def load_ast()
line = readline()
Line 158:
right = load_ast()
return make_node(node_type, left, right)
</syntaxhighlight>
; Output format - refer to the table above
Line 270:
<br>
As noted in the code, the generated IL is naive - the sample focuses on simplicity.
<
COMMENT
this writes a .NET IL assembler source to standard output.
Line 557:
code header;
gen( code );
code trailer</
{{out}}
<pre>
Line 601:
=={{header|ALGOL W}}==
<
% parse tree nodes %
record node( integer type
Line 966:
genOp0( oHalt );
emitCode
end.</
{{out}}
The While Counter example
Line 995:
=={{header|AWK}}==
Tested with gawk 4.1.1 and mawk 1.3.4.
<syntaxhighlight lang="awk">
function error(msg) {
printf("%s\n", msg)
Line 1,276:
list_code()
}
</syntaxhighlight>
{{out|case=count}}
<b>
Line 1,304:
=={{header|C}}==
Tested with gcc 4.81 and later, compiles warning free with -Wall -Wextra
<
#include <stdio.h>
#include <string.h>
Line 1,677:
return 0;
}</
{{out|case=While counter example}}
Line 1,707:
Code by Steve Williams. Tested with GnuCOBOL 2.2.
<
identification division.
*> this code is dedicated to the public domain
Line 2,358:
.
end program showhex.
end program generator.</
{{out|case=Count}}
Line 2,386:
=={{header|Forth}}==
Tested with Gforth 0.7.3
<
: PEEK BUF @ 0= IF KEY BUF ! THEN BUF @ ;
: GETC PEEK 0 BUF ! ;
Line 2,514:
DUP 5 < IF CELLS .INT + @ EXECUTE ELSE DROP THEN CR
REPEAT DROP R> DROP ;
GENERATE EMIT BYE</
Passes all tests.
Line 2,521:
Fortran 2008/2018 code with C preprocessing. On case-sensitive systems, if you call the source file gen.F90, with a capital F, then gfortran will know to use the C preprocessor.
<
use, intrinsic :: iso_fortran_env, only: int32
use, intrinsic :: iso_fortran_env, only: int64
Line 4,401:
end subroutine print_usage
end program gen</
{{out}}
Line 4,429:
=={{header|Go}}==
{{trans|C}}
<
import (
Line 4,821:
codeFinish()
listCode()
}</
{{out}}
Line 4,852:
Implementation:
<
(opcodes)=: opcodes=: ;:{{)n
Line 4,983:
gen_code load_ast y
list_code gen_op halt
}}</
Count example:
<syntaxhighlight lang="j">
count=:{{)n
count = 1;
Line 5,017:
60 jmp (-51) 10
65 halt
</syntaxhighlight>
=={{header|Java}}==
{{trans|Python}}
<
import java.io.File;
Line 5,363:
}
}
</syntaxhighlight>
=={{header|Julia}}==
<
mutable struct Asm32
Line 5,528:
compiletoasm(iob)
</
Datasize: 1 Strings: 2
"count is: "
Line 5,553:
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module CodeGenerator (s$){
Function code$(op$) {
Line 5,778:
Integer 1
}
</syntaxhighlight>
{{out}}
Line 5,807:
=={{header|Nim}}==
<
type
Line 6,117:
if toClose: stream.close()
codegen.run(ast)</
{{out}}
Line 6,231:
=={{header|Perl}}==
Tested with perl v5.26.1
<
use strict; # gen.pl - flatAST to stack machine code
Line 6,270:
print "Datasize: $namecount Strings: $stringcount\n";
print "$_\n" for sort { $strings{$a} <=> $strings{$b} } keys %strings;
print;</
Passes all tests.
Line 6,276:
Reusing parse.e from the [[Compiler/syntax_analyzer#Phix|Syntax Analyzer task]]<br>
Deviates somewhat from the task specification in that it generates executable machine code.
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Compiler\cgen.e
Line 6,670:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<!--</
And a simple test driver for the specific task:
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Compiler\cgen.exw
Line 6,868:
<span style="color: #000080;font-style:italic;">--main(command_line())</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"gcd.c"</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 6,906:
=={{header|Python}}==
Tested with Python 2.7 and 3.x
<
import sys, struct, shlex, operator
Line 7,159:
code_gen(n)
code_finish()
list_code()</
{{out|case=While counter example}}
Line 7,190:
Using 'while-count' example, input used is here: [https://github.com/SqrtNegInf/Rosettacode-Perl6-Smoke/blob/master/ref/ast.txt ast.txt]
{{trans|Perl}}
<syntaxhighlight lang="raku"
Less lt LessEqual le Multiply mul Subtract sub NotEqual ne
Divide div GreaterEqual ge Equal eq Greater gt Negate neg
Line 7,226:
say "Datasize: $name-count Strings: $string-count\n"
~ join('', %strings.keys.sort.reverse «~» "\n")
~ $code;</
{{out}}
<pre>Datasize: 1 Strings: 2
Line 7,256:
<
#
# The Rosetta Code code generator in Ratfor 77.
Line 8,712:
end
######################################################################</
{{out}}
Line 8,784:
The following code implements a code generator for the output of the [http://rosettacode.org/wiki/Compiler/syntax_analyzer#Scala parser].
<
package xyz.hyperreal.rosettacodeCompiler
Line 8,940:
}
</syntaxhighlight>
=={{header|Scheme}}==
<
(import (scheme base)
(scheme file)
Line 9,132:
(generate-code (read-code (cadr (command-line))))
(display "Error: pass an ast filename\n"))
</syntaxhighlight>
Tested on all examples in [[Compiler/Sample programs]].
Line 9,142:
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
<
import "/crypto" for Bytes
import "/fmt" for Fmt
Line 9,486:
codeGen.call(loadAst.call())
codeFinish.call()
listCode.call()</
{{out}}
Line 9,514:
=={{header|Zig}}==
<
const std = @import("std");
Line 10,023:
}
}
</syntaxhighlight>
=={{header|zkl}}==
{{trans|Python}}
<
const WORD_SIZE=4;
Line 10,139:
code.insert(0,66,text.len(),text);
})
}</
<
all_ops,nthString := all_syms.pump(Dictionary(),"reverse"),-1;
println("Datasize: %d bytes, Strings: %d bytes"
Line 10,174:
}
}
}</
<
line:=file.readln().strip(); // one or two tokens
if(line[0]==";") return(Void);
Line 10,186:
left,right := load_ast(file),load_ast(file);
Node(type,Void,left,right)
}</
<
code:=asm(ast,Data());
code_finish(code);
unasm(code);
File("code.bin","wb").write(code);
println("Wrote %d bytes to code.bin".fmt(code.len()));</
File ast.txt is the text at the start of this task.
{{out}}
|