This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,4 @@
FUNCTION: char* strdup ( c-string s ) ;
: my-strdup ( str -- str' )
strdup [ utf8 alien>string ] [ (free) ] bi ;

View file

@ -0,0 +1,4 @@
require 'dll'
strdup=: 'msvcrt.dll _strdup >x *' cd <
free=: 'msvcrt.dll free n x' cd <
getstr=: free ] memr@,&0 _1

View file

@ -0,0 +1,2 @@
getstr@strdup 'Hello World!'
Hello World!

View file

@ -0,0 +1,24 @@
Section Header
+ name := TEST_C_INTERFACE;
// this will be inserted in front of the program
- external := `#include <string.h>`;
Section Public
- main <- (
+ s : STRING_CONSTANT;
+ p : NATIVE_ARRAY[CHARACTER];
s := "Hello World!";
p := s.to_external;
// this will be inserted in-place
// use `expr`:type to tell Lisaac what's the type of the external expression
p := `strdup(@p)` : NATIVE_ARRAY[CHARACTER];
s.print;
'='.print;
p.println;
// this will also be inserted in-place, expression type disregarded
`free(@p)`;
);

View file

@ -0,0 +1,3 @@
> strdup := define_external( strdup, s::string, RETURN::string, LIB = "/lib/libc.so.6" ):
> strdup( "foo" );
"foo"

View file

@ -0,0 +1,11 @@
> csin := define_external( sin, s::float[8], RETURN::float[8], LIB = "libm.so" );
csin := proc(s::numeric)
option call_external, define_external(sin, s::float[8],
RETURN::float[8], LIB = "libm.so");
call_external(
Array(1..8, [...], datatype = integer[4], readonly), false,
args)
end proc
> csin( evalf( Pi / 2 ) );
1.

View file

@ -0,0 +1,3 @@
Needs["NETLink`"];
externalstrdup = DefineDLLFunction["_strdup", "msvcrt.dll", "string", {"string"}];
Print["Duplicate: ", externalstrdup["Hello world!"]]

View file

@ -0,0 +1,9 @@
/* Maxima is written in Lisp and can call Lisp functions.
Use load("funcs.lisp"), or inside Maxima: */
to_lisp();
> (defun $f (a b) (+ a b))
> (to-maxima)
f(5, 6);
11

View file

@ -0,0 +1,68 @@
#include <vga.h>
int Initialize (void)
{
if ( vga_init () == 0 )
return 1;
else
return 0;
}
void SetMode (int newmode)
{
vga_setmode (newmode);
}
int GetMode (void)
{
return vga_getcurrentmode ();
}
int MaxWidth (void)
{
return vga_getxdim ();
}
int MaxHeight (void)
{
return vga_getydim ();
}
void Clear (void)
{
vga_clear ();
}
void SetColour (int colour)
{
vga_setcolor (colour);
}
void SetEGAcolour (int colour)
{
vga_setegacolor (colour);
}
void SetRGB (int red, int green, int blue)
{
vga_setrgbcolor (red, green, blue);
}
void DrawLine (int x0, int y0, int dx, int dy)
{
vga_drawline (x0, y0, x0 + dx, y0 + dy);
}
void Plot (int x, int y)
{
vga_drawpixel (x, y);
}
int ThisColour (int x, int y)
{
return vga_getpixel (x, y);
}
void GetKey (char *ch)
{
*ch = vga_getkey ();
}

View file

@ -0,0 +1,32 @@
FOREIGN MODULE Vga;
TYPE EGAcolour = (black, blue, green, cyan, red, pink, brown, white,
GREY, BLUE, GREEN, CYAN, RED, MAGENTA, YELLOW, WHITE);
PROCEDURE Initialize () : BOOLEAN;
PROCEDURE MaxWidth () : CARDINAL;
PROCEDURE MaxHeight () : CARDINAL;
PROCEDURE Clear;
PROCEDURE SetColour (colour : CARDINAL);
PROCEDURE SetEGAcolour (colour : CARDINAL);
PROCEDURE SetRGB (red, green, blue : CARDINAL);
PROCEDURE DrawLine (x0, y0, dx, dy : CARDINAL);
PROCEDURE Plot (x, y : CARDINAL);
PROCEDURE ThisColour (x, y : CARDINAL) : CARDINAL;
PROCEDURE SetMode (newmode : CARDINAL);
PROCEDURE GetMode () : CARDINAL;
PROCEDURE GetKey (VAR ch : CHAR);
END Vga.

View file

@ -0,0 +1,33 @@
MODULE svg01;
FROM InOut IMPORT Read, Write, WriteBf, WriteString;
IMPORT Vga;
VAR OldMode, x, y : CARDINAL;
ch : CHAR;
BEGIN
IF Vga.Initialize () = FALSE THEN
WriteString ('Could not start SVGAlib libraries. Aborting...');
WriteBf;
HALT
END;
OldMode := Vga.GetMode ();
Vga.SetMode (4);
Vga.SetColour (14);
Vga.Clear ();
Vga.SetColour (10);
FOR y := 125 TO 175 DO
FOR x := 100 TO 500 DO
Vga.Plot (x, y)
END
END;
LOOP
Read (ch);
IF ch = 'X' THEN EXIT END
END;
Vga.SetMode (OldMode);
Write (ch);
WriteBf;
END svg01.

View file

@ -0,0 +1,17 @@
UNSAFE MODULE Foreign EXPORTS Main;
IMPORT IO, Ctypes, Cstring, M3toC;
VAR string1, string2: Ctypes.const_char_star;
BEGIN
string1 := M3toC.CopyTtoS("Foobar");
string2 := M3toC.CopyTtoS("Foobar2");
IF Cstring.strcmp(string1, string2) = 0 THEN
IO.Put("string1 = string2\n");
ELSE
IO.Put("string1 # string2\n");
END;
M3toC.FreeCopiedS(string1);
M3toC.FreeCopiedS(string2);
END Foreign.