Compiler/virtual machine interpreter: Difference between revisions
Compiler/virtual machine interpreter (view source)
Revision as of 20:36, 26 August 2022
, 1 year agosyntax 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 153:
; A simple example virtual machine:
<
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</
; Additional examples
Line 219:
<syntaxhighlight lang="ada">--
-- The Rosetta Code Virtual Machine, in Ada.
--
Line 1,153:
Set_Exit_Status (status);
end VM;</
Line 1,169:
=={{header|Aime}}==
<syntaxhighlight lang="text">integer n, pc, sp;
file f;
text s;
Line 1,255:
isk_greater(code, pc, pc);
}
}</
=={{header|ALGOL W}}==
<
% string literals %
string(256) array stringValue ( 0 :: 256 );
Line 1,564:
end while_not_halted
end
end.</
=={{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.)
<
Usage: vm [INPUTFILE [OUTPUTFILE]]
If INPUTFILE or OUTPUTFILE is "-" or missing, then standard input
Line 3,377:
}
(********************************************************************)</
{{out}}
Line 3,393:
=={{header|AWK}}==
Tested with gawk 4.1.1 and mawk 1.3.4.
<syntaxhighlight lang="awk">
function error(msg) {
printf("%s\n", msg)
Line 3,548:
run_vm(data_size)
}
</syntaxhighlight>
{{out|case=count}}
<b>
Line 3,566:
=={{header|C}}==
Tested with gcc 4.81 and later, compiles warning free with -Wall -Wextra
<
#include <stdlib.h>
#include <stdarg.h>
Line 3,832:
int data[1000 + data_size];
run_vm(object, data, data_size, string_pool);
}</
=={{header|COBOL}}==
Code by Steve Williams (with changes to work around code highlighting issues). Tested with GnuCOBOL 2.2.
<
identification division.
*> this code is dedicated to the public domain
Line 4,258:
end program emitword.
end program vminterpreter.</
{{out|case=Count}}
Line 4,282:
<
#|-*- mode:lisp -*-|#
#|
Line 5,006:
(uiop:quit 0)))
;;; vim: set ft=lisp lisp:</
Line 5,032:
<syntaxhighlight lang="d">//
// The Rosetta Code Virtual Machine in D.
//
Line 5,938:
return 0;
}</
Line 5,957:
=={{header|Forth}}==
Tested with Gforth 0.7.3
<
: 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</
=={{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.
<
use, intrinsic :: iso_fortran_env, only: int32
use, intrinsic :: iso_fortran_env, only: int64
Line 7,577:
end subroutine print_usage
end program vm</
{{out}}
Line 7,593:
=={{header|Go}}==
{{trans|Python}}
<
import (
Line 7,895:
scanner = bufio.NewScanner(codeGen)
runVM(loadCode())
}</
{{out}}
Line 7,913:
=={{header|Icon}}==
{{trans|ObjectIcon}}
<
#
# The Rosetta Code virtual machine in Icon. Migrated from the
Line 8,287:
write(&errout, "Bad opcode.")
exit(1)
end</
{{out}}
Line 8,303:
=={{header|J}}==
Implementation:
<
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.
}}</
Task example:
<
count = 1;
while (count < 10) {
Line 8,399:
count is: 8
count is: 9
</syntaxhighlight>
=={{header|Julia}}==
<
code::Vector{UInt8}
stack::Vector{Int32}
Line 8,505:
const vm = assemble(iob)
runvm(vm)
</
count is: 1
count is: 2
Line 8,519:
=={{header|M2000 Interpreter}}==
===Using Select Case===
<syntaxhighlight lang="m2000 interpreter">
Module Virtual_Machine_Interpreter (a$){
\\ function to extract string, replacing escape codes.
Line 8,692:
65 halt
}
</syntaxhighlight>
===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">
Module Virtual_Machine_Interpreter (a$){
\\ function to extract string, replacing escape codes.
Line 8,847:
65 halt
}
</syntaxhighlight>
=={{header|Mercury}}==
Line 8,860:
<
%%%
%%% The Rosetta Code Virtual Machine, in Mercury.
Line 9,751:
%%% prolog-indent-width: 2
%%% end:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</
Line 9,773:
=={{header|Nim}}==
<
type
Line 10,093:
vm.load(code)
vm.run()</
All tests passed.
=={{header|ObjectIcon}}==
<
#
# The Rosetta Code virtual machine in Object Icon.
Line 10,505:
exit(1)
end
end</
{{out}}
Line 10,521:
=={{header|Perl}}==
Tested with perl v5.26.1
<
# http://www.rosettacode.org/wiki/Compiler/virtual_machine_interpreter
Line 10,570:
}
$ops[vec($binary, $pc++, 8)][1]->() while 1; # run it</
Passes all tests.
=={{header|Phix}}==
Reusing cgen.e from the [[Compiler/code_generator#Phix|Code Generator task]]
<!--<
<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>
<!--</
{{out}}
<pre>
Line 10,633:
<
%%%
%%% The Rosetta Code Virtual Machine, for GNU Prolog.
Line 10,999:
%%% prolog-indent-width: 2
%%% end:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</
Line 11,016:
=={{header|Python}}==
Tested with Python 2.7 and 3.x
<
import sys, struct
Line 11,183:
data_size = load_code()
run_vm(data_size)</
=={{header|Racket}}==
Line 11,194:
<
;;;
;;; The Rosetta Code Virtual Machine, in Typed Racket.
Line 11,941:
(close-output-port outf))
(exit 0)))))</
Line 11,966:
{{trans|Perl}}
<syntaxhighlight lang="raku"
Datasize: 3 Strings: 2
"count is: "
Line 12,043:
$pc += $w;
%ops{%n2op{ $opcode }}();
}</
{{out}}
<pre>count is: 1
Line 12,061:
<
#
# The Rosetta Code code virtual machine in Ratfor 77.
Line 13,293:
end
######################################################################</
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].
<
package xyz.hyperreal.rosettacodeCompiler
Line 13,547:
}
</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 13,579:
}
</syntaxhighlight>
=={{header|Scheme}}==
Line 13,587:
All of the "Compiler/Sample programs" are correctly interpreted.
<
(import (scheme base)
(scheme char)
Line 13,750:
(run-program data strings code))
(display "Error: pass a .asm filename\n"))
</syntaxhighlight>
=={{header|Wren}}==
Line 13,758:
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
<
import "/crypto" for Bytes
import "/fmt" for Conv
Line 14,024:
lines = FileUtil.readLines("codegen.txt")
lineCount = lines.count
runVM.call(loadCode.call())</
{{out}}
Line 14,040:
=={{header|Zig}}==
<
const std = @import("std");
Line 14,363:
}
}
</syntaxhighlight>
=={{header|zkl}}==
{{trans|Python}}
File rvm.zkl:
<
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);</
The binary code file code.bin:
{{out}}
|