Compiler/code generator: Difference between revisions

m
syntax highlighting fixup automation
m (Reverted edits by Jwells1213 (talk) to last revision by Chemoelectric)
m (syntax highlighting fixup automation)
Line 34:
|-
| style="vertical-align:top" |
<langsyntaxhighlight lang="c">count = 1;
while (count < 10) {
print("count is: ", count, "\n");
count = count + 1;
}</langsyntaxhighlight>
 
| style="vertical-align:top" |
Line 136:
Loading this data into an internal parse tree should be as simple as:
 
<langsyntaxhighlight lang="python">
def load_ast()
line = readline()
Line 158:
right = load_ast()
return make_node(node_type, left, right)
</syntaxhighlight>
</lang>
 
; 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.
<langsyntaxhighlight lang="algol68"># RC Compiler code generator #
COMMENT
this writes a .NET IL assembler source to standard output.
Line 557:
code header;
gen( code );
code trailer</langsyntaxhighlight>
{{out}}
<pre>
Line 601:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % code generator %
% parse tree nodes %
record node( integer type
Line 966:
genOp0( oHalt );
emitCode
end.</langsyntaxhighlight>
{{out}}
The While Counter example
Line 995:
=={{header|AWK}}==
Tested with gawk 4.1.1 and mawk 1.3.4.
<syntaxhighlight lang="awk">
<lang AWK>
function error(msg) {
printf("%s\n", msg)
Line 1,276:
list_code()
}
</syntaxhighlight>
</lang>
{{out|case=count}}
<b>
Line 1,304:
=={{header|C}}==
Tested with gcc 4.81 and later, compiles warning free with -Wall -Wextra
<langsyntaxhighlight Clang="c">#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Line 1,677:
 
return 0;
}</langsyntaxhighlight>
 
{{out|case=While counter example}}
Line 1,707:
Code by Steve Williams. Tested with GnuCOBOL 2.2.
 
<langsyntaxhighlight lang="cobol"> >>SOURCE FORMAT IS FREE
identification division.
*> this code is dedicated to the public domain
Line 2,358:
.
end program showhex.
end program generator.</langsyntaxhighlight>
 
{{out|case=Count}}
Line 2,386:
=={{header|Forth}}==
Tested with Gforth 0.7.3
<langsyntaxhighlight Forthlang="forth">CREATE BUF 0 ,
: 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</langsyntaxhighlight>
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.
 
<langsyntaxhighlight lang="fortran">module compiler_type_kinds
use, intrinsic :: iso_fortran_env, only: int32
use, intrinsic :: iso_fortran_env, only: int64
Line 4,401:
end subroutine print_usage
end program gen</langsyntaxhighlight>
 
{{out}}
Line 4,429:
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 4,821:
codeFinish()
listCode()
}</langsyntaxhighlight>
 
{{out}}
Line 4,852:
 
Implementation:
<langsyntaxhighlight Jlang="j">require'format/printf'
 
(opcodes)=: opcodes=: ;:{{)n
Line 4,983:
gen_code load_ast y
list_code gen_op halt
}}</langsyntaxhighlight>
 
Count example:
<syntaxhighlight lang="j">
<lang J>
count=:{{)n
count = 1;
Line 5,017:
60 jmp (-51) 10
65 halt
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
{{trans|Python}}
<langsyntaxhighlight lang="java">package codegenerator;
 
import java.io.File;
Line 5,363:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">import Base.show
 
mutable struct Asm32
Line 5,528:
 
compiletoasm(iob)
</langsyntaxhighlight>{{output}}<pre>
Datasize: 1 Strings: 2
"count is: "
Line 5,553:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CodeGenerator (s$){
Function code$(op$) {
Line 5,778:
Integer 1
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 5,807:
=={{header|Nim}}==
 
<langsyntaxhighlight Nimlang="nim">import os, re, streams, strformat, strutils, tables, std/decls
 
type
Line 6,117:
if toClose: stream.close()
 
codegen.run(ast)</langsyntaxhighlight>
 
{{out}}
Line 6,231:
=={{header|Perl}}==
Tested with perl v5.26.1
<langsyntaxhighlight Perllang="perl">#!/usr/bin/perl
 
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;</langsyntaxhighlight>
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.
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<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>
<!--</langsyntaxhighlight>-->
And a simple test driver for the specific task:
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 6,906:
=={{header|Python}}==
Tested with Python 2.7 and 3.x
<langsyntaxhighlight Pythonlang="python">from __future__ import print_function
import sys, struct, shlex, operator
 
Line 7,159:
code_gen(n)
code_finish()
list_code()</langsyntaxhighlight>
 
{{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" perl6line>my %opnames = <
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;</langsyntaxhighlight>
{{out}}
<pre>Datasize: 1 Strings: 2
Line 7,256:
 
 
<langsyntaxhighlight lang="ratfor">######################################################################
#
# The Rosetta Code code generator in Ratfor 77.
Line 8,712:
end
 
######################################################################</langsyntaxhighlight>
 
{{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].
 
<langsyntaxhighlight lang="scala">
package xyz.hyperreal.rosettacodeCompiler
 
Line 8,940:
 
}
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme file)
Line 9,132:
(generate-code (read-code (cadr (command-line))))
(display "Error: pass an ast filename\n"))
</syntaxhighlight>
</lang>
 
Tested on all examples in [[Compiler/Sample programs]].
Line 9,142:
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
<langsyntaxhighlight lang="ecmascript">import "/dynamic" for Enum, Struct, Tuple
import "/crypto" for Bytes
import "/fmt" for Fmt
Line 9,486:
codeGen.call(loadAst.call())
codeFinish.call()
listCode.call()</langsyntaxhighlight>
 
{{out}}
Line 9,514:
 
=={{header|Zig}}==
<langsyntaxhighlight lang="zig">
const std = @import("std");
 
Line 10,023:
}
}
</syntaxhighlight>
</lang>
 
=={{header|zkl}}==
{{trans|Python}}
<langsyntaxhighlight lang="zkl">// This is a little endian machine
 
const WORD_SIZE=4;
Line 10,139:
code.insert(0,66,text.len(),text);
})
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn unasm(code){
all_ops,nthString := all_syms.pump(Dictionary(),"reverse"),-1;
println("Datasize: %d bytes, Strings: %d bytes"
Line 10,174:
}
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn load_ast(file){
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)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">ast:=load_ast(File(vm.nthArg(0)));
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()));</langsyntaxhighlight>
File ast.txt is the text at the start of this task.
{{out}}
10,327

edits