Compiler/AST interpreter: 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 11:
;Loading the AST from the syntax analyzer is as simple as (pseudo code):
<
line = readline()
# Each line has at least one token
Line 31:
left = load_ast()
right = load_ast()
return make_node(node_type, left, right)</
; The interpreter algorithm is relatively simple:
<
if x == NULL return NULL
elif x.node_type == Integer return x.value converted to an integer
Line 66:
return NULL
else
error("unknown node type")</
Notes:
Line 88:
|-
| style="vertical-align:top" |
<
Simple prime number generator
*/
Line 107:
}
}
print("Total primes found: ", count, "\n"); </
| style="vertical-align:top" |
Line 160:
=={{header|ALGOL W}}==
<
% parse tree nodes %
record node( integer type
Line 432:
% parse the output from the syntax analyser and intetrpret parse tree %
eval( readNode )
end.</
{{out}}
<pre>
Line 449:
=={{header|C}}==
Tested with gcc 4.81 and later, compiles warning free with -Wall -Wextra
<
#include <stdio.h>
#include <string.h>
Line 709:
return 0;
}</
{{out|case=prime numbers output from AST interpreter}}
Line 747:
Code by Steve Williams. Tested with GnuCOBOL 2.2.
<
identification division.
*> this code is dedicated to the public domain
Line 1,243:
.
end program reporterror.
end program astinterpreter.</
{{out|case=Primes}}
Line 1,276:
=={{header|Forth}}==
Tested with Gforth 0.7.3
<
: PEEK BUF @ 0= IF KEY BUF ! THEN BUF @ ;
: GETC PEEK 0 BUF ! ;
Line 1,365:
GETAST INTERP
</syntaxhighlight>
Passes all tests.
Line 1,372:
The code is Fortran 2008/2018 with the C preprocessor. On case-sensitive systems, you can name the source file Interp.F90, with a capital F, so gfortran will know (without an option flag) to invoke the C preprocessor.
<
!!! An implementation of the Rosetta Code interpreter task:
!!! https://rosettacode.org/wiki/Compiler/AST_interpreter
Line 2,829:
end subroutine print_usage
end program Interp</
{{out}}
Line 2,862:
=={{header|Go}}==
{{trans|C}}
<
import (
Line 3,157:
x := loadAst()
interp(x)
}</
{{out}}
Line 3,194:
Implementation:
<
emit=:{{
outbuf=: outbuf,y
Line 3,260:
end.
}}
</syntaxhighlight>
Task example:
<
/*
Simple prime number generator
Line 3,315:
Total primes found: 26
</
=={{header|Java}}==
<
import java.util.Scanner;
import java.io.File;
Line 3,558:
}
</syntaxhighlight>
=={{header|Julia}}==
<
node_type::String
left::Union{Nothing, Anode}
Line 3,728:
interp(load_ast(lio))
</
3 is prime
5 is prime
Line 3,761:
Using AST produced by the parser from the task “syntax analyzer”.
<
import ast_parser
Line 3,924:
if toClose: stream.close()
discard ast.interp()</
{{out}}
Line 3,974:
Tested with perl v5.26.1
<
use strict; # interpreter.pl - execute a flatAST
Line 4,018:
sub Sequence::run { $_->run for $_[0]->@* }
sub Subtract::run { $_[0][0]->run - $_[0][1]->run }
sub While::run { $_[0][1]->run while $_[0][0]->run }</
Passes all tests.
=={{header|Phix}}==
Reusing parse.e from the [[Compiler/syntax_analyzer#Phix|Syntax Analyzer task]]
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Compiler\interp.exw
Line 4,093:
<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;">"primes.c"</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 4,126:
=={{header|Python}}==
Tested with Python 2.7 and 3.x
<
import sys, shlex, operator
Line 4,290:
n = load_ast()
interp(n)</
{{out|case=prime numbers output from AST interpreter}}
Line 4,331:
<
#
# The Rosetta Code AST interpreter in Ratfor 77.
Line 5,615:
end
######################################################################</
{{out}}
Line 5,653:
The following code implements an interpreter for the output of the [http://rosettacode.org/wiki/Compiler/syntax_analyzer#Scala parser].
<
package xyz.hyperreal.rosettacodeCompiler
Line 5,731:
}
</syntaxhighlight>
The above code depends on the function <tt>unescape()</tt> to perform string escape sequence translation. That function is defined in the following separate source file.
<
package xyz.hyperreal
Line 5,763:
}
</syntaxhighlight>
=={{header|Scheme}}==
<
(import (scheme base)
(scheme file)
Line 5,905:
(run-program (read-code (cadr (command-line))))
(display "Error: pass an ast filename\n"))
</syntaxhighlight>
{{out}}
Line 5,945:
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
<
import "/fmt" for Conv
import "/ioutil" for FileUtil
Line 6,166:
lineCount = lines.count
var x = loadAst.call()
interp.call(x)</
{{out}}
Line 6,199:
=={{header|Zig}}==
<
const std = @import("std");
Line 6,515:
}
}
</syntaxhighlight>
=={{header|zkl}}==
<
const FETCH=N, STORE=N, PUSH=N, ADD=N, SUB=N, MUL=N, DIV=N, MOD=N,
LT=N, GT=N, LE=N, GE=N, EQ=N, NE=N,
Line 6,576:
}
Void
}</
<
line:=file.readln().strip(); // one or two tokens
if(line[0]==";") return(Void);
Line 6,589:
left,right := load_ast(file),load_ast(file);
Node(type,Void,left,right)
}</
<
runNode(ast);</
{{out}}
<pre>
|