Compiler/AST interpreter: 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 11:
;Loading the AST from the syntax analyzer is as simple as (pseudo code):
 
<langsyntaxhighlight lang="python">def load_ast()
line = readline()
# Each line has at least one token
Line 31:
left = load_ast()
right = load_ast()
return make_node(node_type, left, right)</langsyntaxhighlight>
 
; The interpreter algorithm is relatively simple:
 
<langsyntaxhighlight lang="python">interp(x)
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")</langsyntaxhighlight>
 
Notes:
Line 88:
|-
| style="vertical-align:top" |
<langsyntaxhighlight lang="c">/*
Simple prime number generator
*/
Line 107:
}
}
print("Total primes found: ", count, "\n"); </langsyntaxhighlight>
 
| style="vertical-align:top" |
Line 160:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % AST interpreter %
% parse tree nodes %
record node( integer type
Line 432:
% parse the output from the syntax analyser and intetrpret parse tree %
eval( readNode )
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 449:
=={{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 709:
 
return 0;
}</langsyntaxhighlight>
 
{{out|case=prime numbers output from AST interpreter}}
Line 747:
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 1,243:
.
end program reporterror.
end program astinterpreter.</langsyntaxhighlight>
 
{{out|case=Primes}}
Line 1,276:
=={{header|Forth}}==
Tested with Gforth 0.7.3
<langsyntaxhighlight Forthlang="forth">CREATE BUF 0 , \ single-character look-ahead buffer
: PEEK BUF @ 0= IF KEY BUF ! THEN BUF @ ;
: GETC PEEK 0 BUF ! ;
Line 1,365:
 
GETAST INTERP
</syntaxhighlight>
</lang>
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.
 
<langsyntaxhighlight lang="fortran">!!!
!!! 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</langsyntaxhighlight>
 
{{out}}
Line 2,862:
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 3,157:
x := loadAst()
interp(x)
}</langsyntaxhighlight>
 
{{out}}
Line 3,194:
Implementation:
 
<langsyntaxhighlight Jlang="j">outbuf=: ''
emit=:{{
outbuf=: outbuf,y
Line 3,260:
end.
}}
</syntaxhighlight>
</lang>
 
Task example:
 
<langsyntaxhighlight Jlang="j">primes=:{{)n
/*
Simple prime number generator
Line 3,315:
Total primes found: 26
 
</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.Scanner;
import java.io.File;
Line 3,558:
}
 
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">struct Anode
node_type::String
left::Union{Nothing, Anode}
Line 3,728:
 
interp(load_ast(lio))
</langsyntaxhighlight>{{output}}<pre>
3 is prime
5 is prime
Line 3,761:
Using AST produced by the parser from the task “syntax analyzer”.
 
<langsyntaxhighlight Nimlang="nim">import os, strutils, streams, tables
 
import ast_parser
Line 3,924:
if toClose: stream.close()
 
discard ast.interp()</langsyntaxhighlight>
 
{{out}}
Line 3,974:
Tested with perl v5.26.1
 
<langsyntaxhighlight Perllang="perl">#!/usr/bin/perl
 
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 }</langsyntaxhighlight>
Passes all tests.
 
=={{header|Phix}}==
Reusing parse.e from the [[Compiler/syntax_analyzer#Phix|Syntax Analyzer task]]
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 4,126:
=={{header|Python}}==
Tested with Python 2.7 and 3.x
<langsyntaxhighlight Pythonlang="python">from __future__ import print_function
import sys, shlex, operator
 
Line 4,290:
 
n = load_ast()
interp(n)</langsyntaxhighlight>
 
{{out|case=prime numbers output from AST interpreter}}
Line 4,331:
 
 
<langsyntaxhighlight lang="ratfor">######################################################################
#
# The Rosetta Code AST interpreter in Ratfor 77.
Line 5,615:
end
 
######################################################################</langsyntaxhighlight>
 
{{out}}
Line 5,653:
The following code implements an interpreter for the output of the [http://rosettacode.org/wiki/Compiler/syntax_analyzer#Scala parser].
 
<langsyntaxhighlight lang="scala">
package xyz.hyperreal.rosettacodeCompiler
 
Line 5,731:
 
}
</syntaxhighlight>
</lang>
 
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.
 
<langsyntaxhighlight lang="scala">
package xyz.hyperreal
 
Line 5,763:
 
}
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme file)
Line 5,905:
(run-program (read-code (cadr (command-line))))
(display "Error: pass an ast filename\n"))
</syntaxhighlight>
</lang>
 
{{out}}
Line 5,945:
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
<langsyntaxhighlight lang="ecmascript">import "/dynamic" for Enum, Struct, Tuple
import "/fmt" for Conv
import "/ioutil" for FileUtil
Line 6,166:
lineCount = lines.count
var x = loadAst.call()
interp.call(x)</langsyntaxhighlight>
 
{{out}}
Line 6,199:
 
=={{header|Zig}}==
<langsyntaxhighlight lang="zig">
const std = @import("std");
 
Line 6,515:
}
}
</syntaxhighlight>
</lang>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">const{ var _n=-1; var[proxy]N=fcn{ _n+=1 }; } // enumerator
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
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn load_ast(file){
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)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">ast:=load_ast(File(vm.nthArg(0)));
runNode(ast);</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits