Compiler/virtual machine 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 153:
; A simple example virtual machine:
 
<langsyntaxhighlight lang="python">def run_vm(data_size)
int stack[data_size + 1000]
set stack[0..data_size - 1] to 0
Line 190:
elif op == PRTS: print the constant string referred to by stack[-1]; stack.pop()
elif op == PRTI: print stack[-1] as an integer; stack.pop()
elif op == HALT: break</langsyntaxhighlight>
 
; Additional examples
Line 219:
 
 
<syntaxhighlight lang="ada">--
<lang Ada>--
-- The Rosetta Code Virtual Machine, in Ada.
--
Line 1,153:
 
Set_Exit_Status (status);
end VM;</langsyntaxhighlight>
 
 
Line 1,169:
 
=={{header|Aime}}==
<syntaxhighlight lang="text">integer n, pc, sp;
file f;
text s;
Line 1,255:
isk_greater(code, pc, pc);
}
}</langsyntaxhighlight>
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % virtual machine interpreter %
% string literals %
string(256) array stringValue ( 0 :: 256 );
Line 1,564:
end while_not_halted
end
end.</langsyntaxhighlight>
 
=={{header|ATS}}==
Line 1,575:
(Without the C optimizer, ATS code can run much, much more slowly. It is worth comparing the Mandelbrot example with and without the optimizer.)
 
<langsyntaxhighlight lang="ats">(*
Usage: vm [INPUTFILE [OUTPUTFILE]]
If INPUTFILE or OUTPUTFILE is "-" or missing, then standard input
Line 3,377:
}
 
(********************************************************************)</langsyntaxhighlight>
 
{{out}}
Line 3,393:
=={{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 3,548:
run_vm(data_size)
}
</syntaxhighlight>
</lang>
{{out|case=count}}
<b>
Line 3,566:
=={{header|C}}==
Tested with gcc 4.81 and later, compiles warning free with -Wall -Wextra
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
Line 3,832:
int data[1000 + data_size];
run_vm(object, data, data_size, string_pool);
}</langsyntaxhighlight>
 
=={{header|COBOL}}==
Code by Steve Williams (with changes to work around code highlighting issues). Tested with GnuCOBOL 2.2.
 
<langsyntaxhighlight lang="cobol"> >>SOURCE FORMAT IS FREE
identification division.
*> this code is dedicated to the public domain
Line 4,258:
end program emitword.
 
end program vminterpreter.</langsyntaxhighlight>
 
{{out|case=Count}}
Line 4,282:
 
 
<langsyntaxhighlight lang="lisp">#!/bin/sh
#|-*- mode:lisp -*-|#
#|
Line 5,006:
(uiop:quit 0)))
 
;;; vim: set ft=lisp lisp:</langsyntaxhighlight>
 
 
Line 5,032:
 
 
<syntaxhighlight lang="d">//
<lang D>//
// The Rosetta Code Virtual Machine in D.
//
Line 5,938:
 
return 0;
}</langsyntaxhighlight>
 
 
Line 5,957:
=={{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 6,039:
: RUN BYTECODE @ A !
BEGIN C@A+ CELLS OPS + @ EXECUTE AGAIN ;
>HEADER >BYTECODE RUN</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|gfortran|11.2.1}}
Fortran 2008/2018 code with some limited use of the C preprocessor. If you are on a platform with case-sensitive filenames, and call the source file vm.F90, 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 7,577:
end subroutine print_usage
end program vm</langsyntaxhighlight>
 
{{out}}
Line 7,593:
=={{header|Go}}==
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 7,895:
scanner = bufio.NewScanner(codeGen)
runVM(loadCode())
}</langsyntaxhighlight>
 
{{out}}
Line 7,913:
=={{header|Icon}}==
{{trans|ObjectIcon}}
<langsyntaxhighlight lang="icon"># -*- Icon -*-
#
# The Rosetta Code virtual machine in Icon. Migrated from the
Line 8,287:
write(&errout, "Bad opcode.")
exit(1)
end</langsyntaxhighlight>
 
{{out}}
Line 8,303:
=={{header|J}}==
Implementation:
<langsyntaxhighlight Jlang="j">(opcodes)=: opcodes=: ;:{{)n
fetch store push add sub mul div mod lt gt le ge
eq ne and or neg not jmp jz prtc prts prti halt
Line 8,378:
pc=: pc+k
end.
}}</langsyntaxhighlight>
 
Task example:
<langsyntaxhighlight Jlang="j">count=:{{)n
count = 1;
while (count < 10) {
Line 8,399:
count is: 8
count is: 9
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">mutable struct VM32
code::Vector{UInt8}
stack::Vector{Int32}
Line 8,505:
const vm = assemble(iob)
runvm(vm)
</langsyntaxhighlight>{{output}}<pre>
count is: 1
count is: 2
Line 8,519:
=={{header|M2000 Interpreter}}==
===Using Select Case===
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Virtual_Machine_Interpreter (a$){
\\ function to extract string, replacing escape codes.
Line 8,692:
65 halt
}
</syntaxhighlight>
</lang>
 
===Using Lambda functions===
Line 8,698:
A call local to function pass the current scope to function, so it's like a call to subroutine, but faster.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Virtual_Machine_Interpreter (a$){
\\ function to extract string, replacing escape codes.
Line 8,847:
65 halt
}
</syntaxhighlight>
</lang>
 
=={{header|Mercury}}==
Line 8,860:
 
 
<langsyntaxhighlight Mercurylang="mercury">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%% The Rosetta Code Virtual Machine, in Mercury.
Line 9,751:
%%% prolog-indent-width: 2
%%% end:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</langsyntaxhighlight>
 
 
Line 9,773:
=={{header|Nim}}==
 
<langsyntaxhighlight Nimlang="nim">import os, parseutils, strutils, strscans, strformat
 
type
Line 10,093:
 
vm.load(code)
vm.run()</langsyntaxhighlight>
 
All tests passed.
 
=={{header|ObjectIcon}}==
<langsyntaxhighlight lang="objecticon"># -*- ObjectIcon -*-
#
# The Rosetta Code virtual machine in Object Icon.
Line 10,505:
exit(1)
end
end</langsyntaxhighlight>
 
{{out}}
Line 10,521:
=={{header|Perl}}==
Tested with perl v5.26.1
<langsyntaxhighlight Perllang="perl">#!/usr/bin/perl
 
# http://www.rosettacode.org/wiki/Compiler/virtual_machine_interpreter
Line 10,570:
}
 
$ops[vec($binary, $pc++, 8)][1]->() while 1; # run it</langsyntaxhighlight>
Passes all tests.
 
=={{header|Phix}}==
Reusing cgen.e from the [[Compiler/code_generator#Phix|Code Generator task]]
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Compiler\vm.exw
Line 10,615:
<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;">"count.c"</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 10,633:
 
 
<langsyntaxhighlight lang="prolog">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%% The Rosetta Code Virtual Machine, for GNU Prolog.
Line 10,999:
%%% prolog-indent-width: 2
%%% end:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</langsyntaxhighlight>
 
 
Line 11,016:
=={{header|Python}}==
Tested with Python 2.7 and 3.x
<langsyntaxhighlight Pythonlang="python">from __future__ import print_function
import sys, struct
 
Line 11,183:
 
data_size = load_code()
run_vm(data_size)</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 11,194:
 
 
<langsyntaxhighlight Racketlang="racket">#lang typed/racket
;;;
;;; The Rosetta Code Virtual Machine, in Typed Racket.
Line 11,941:
(close-output-port outf))
 
(exit 0)))))</langsyntaxhighlight>
 
 
Line 11,966:
 
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line>my @CODE = q:to/END/.lines;
Datasize: 3 Strings: 2
"count is: "
Line 12,043:
$pc += $w;
%ops{%n2op{ $opcode }}();
}</langsyntaxhighlight>
{{out}}
<pre>count is: 1
Line 12,061:
 
 
<langsyntaxhighlight lang="ratfor">######################################################################
#
# The Rosetta Code code virtual machine in Ratfor 77.
Line 13,293:
end
 
######################################################################</langsyntaxhighlight>
 
 
Line 13,330:
The following code implements a virtual machine for the output of the [http://rosettacode.org/wiki/Compiler/code_generator#Scala code generator].
 
<langsyntaxhighlight lang="scala">
package xyz.hyperreal.rosettacodeCompiler
 
Line 13,547:
 
}
</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 13,579:
 
}
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
Line 13,587:
All of the "Compiler/Sample programs" are correctly interpreted.
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme char)
Line 13,750:
(run-program data strings code))
(display "Error: pass a .asm filename\n"))
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
Line 13,758:
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
<langsyntaxhighlight lang="ecmascript">import "/dynamic" for Enum
import "/crypto" for Bytes
import "/fmt" for Conv
Line 14,024:
lines = FileUtil.readLines("codegen.txt")
lineCount = lines.count
runVM.call(loadCode.call())</langsyntaxhighlight>
 
{{out}}
Line 14,040:
 
=={{header|Zig}}==
<langsyntaxhighlight lang="zig">
const std = @import("std");
 
Line 14,363:
}
}
</syntaxhighlight>
</lang>
 
=={{header|zkl}}==
{{trans|Python}}
File rvm.zkl:
<langsyntaxhighlight lang="zkl">// This is a little endian machine
const WORD_SIZE=4;
const{ var _n=-1; var[proxy]N=fcn{ _n+=1 } } // enumerator
Line 14,429:
code.del(0,sz+2);
}
run_vm(code,1000);</langsyntaxhighlight>
The binary code file code.bin:
{{out}}
10,333

edits