git @ Cat's Eye Technologies Maentwrog / rel_1_1_2011_0426
Import of Maentwrog version 1.1 revision 2011.0426 sources. Cat's Eye Technologies 10 years ago
8 changed file(s) with 165 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
0 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1 <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
2 <head>
3 <title>The Maentwrog Programming Language</title>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5 <!-- begin html doc dynamic markup -->
6 <script type="text/javascript" src="/contrib/jquery-1.6.4.min.js"></script>
7 <script type="text/javascript" src="/scripts/documentation.js"></script>
8 <!-- end html doc dynamic markup -->
9 </head>
10 <body>
11
12 <h1>The Maentwrog Programming Language</h1>
13
14 <p>This is a rudimentary specification for the Maentwrog programming language.</p>
15
16 <p>This information, and the example programs in this distribution, were taken
17 from the esolangs.org wiki page for Maentwrog, written by User:Marinus and
18 (like all esowiki articles) placed in the public domain. Thanks Marinus!</p>
19
20 <h2>Syntax</h2>
21
22 <p>A Maentwrog program consists of a series of words, separated by whitespace. Words can contain any character except whitespace.
23 The way these words are executed depends on the character they begin with.</p>
24
25 <ul>
26 <li>A word that consists of digits is taken as an integer and pushed. A minus sign
27 may be used to make negative numbers, so <code>25</code> and <code>-14</code> are
28 words that push 25 and -14 onto the stack. Extra characters at the end of the word are allowed,
29 so <code>25abc</code> and <code>25.14</code> both also push 25. </li>
30 <li>A word that isn't a number is treated as either a function or a variable. If the word is defined as a function, it is executed; if it's a variable,
31 its current value is pushed to the stack. Using an undefined word results in an error, though this doesn't stop further execution of the program. </li>
32 <li>To define a word as a function, use the syntax <code>: new-word words to execute ;</code> (as in Forth). Redefining a word is not allowed,
33 and neither are nested function definitions. </li>
34 <li>To define a word as a variable, use the syntax <code>*varname</code>. This must be done before using a variable. </li>
35 </ul>
36
37 <p>Additionally, words (except number words) can take one of a list of prefixes, changing how the word is executed.</p>
38
39 <table>
40 <tr>
41 <td> <b>Prefix</b> </td><td> <b>Action</b> </td><td> <b>Example</b> </td><td> <b>Result</b>
42 </td></tr>
43 <tr>
44 <td> <code>=</code> </td><td> Assign to variable </td><td> <code>=foo</code> </td><td> A value popped from the stack is assigned to the variable <i>foo</i>.
45 </td></tr>
46 <tr>
47 <td> <code>@</code> </td><td> If </td><td> <code>@bye</code> </td><td> Pop value, stop the program (see predefined words) if it isn't 0.
48 </td></tr>
49 <tr>
50 <td> <code>[</code> </td><td> While </td><td> <code>[xyz</code> </td><td> Pop value, if it's not 0 execute word <i>xyz</i>, then pop another value and do it again; continue until a 0 is popped.
51 </td></tr>
52 <tr>
53 <td> <code>$</code> </td><td> Repeat </td><td> <code>$.</code> </td><td> Pop value, then output that many values from the stack.
54 </td></tr></table>
55
56 <h3>Predefined words</h3>
57
58 <table>
59 <tr>
60 <td> <b>Word</b> </td><td> <b>Stack effect</b> </td><td> <b>Description</b>
61 </td></tr>
62 <tr>
63 <td> <code>bye</code> </td><td> </td><td> Stop program immediately.
64 </td></tr>
65 <tr>
66 <td> <code>rem</code> ... <code>;</code> </td><td> </td><td> Comment. (Ignore all words between <code>rem</code> and <code>;</code>.)
67 </td></tr>
68 <tr>
69 <td> <code>:</code> <i>word</i> ... <code>;</code> </td><td> </td><td> Define a new word.
70 </td></tr>
71 <tr>
72 <td> <code>debug</code> </td><td> </td><td> Turn on debugging (outputs all words executed).
73 </td></tr>
74 <tr>
75 <td> <code>vars</code> </td><td> </td><td> Output a list of currently defined variables and their values.
76 </td></tr>
77 <tr>
78 <td> <code>words</code> </td><td> </td><td> Output a list of currently defined words.
79 </td></tr>
80 <tr>
81 <td> <code>alloc</code> </td><td> <code>n</code> | <code>ptr</code> </td><td> Allocate memory for <i>n</i> C <i>long</i>s, returns a pointer.
82 </td></tr>
83 <tr>
84 <td> <code>free</code> </td><td> <code>ptr</code> | <code>-</code> </td><td> Free memory at pointer.
85 </td></tr>
86 <tr>
87 <td> <code>size</code> </td><td> <code>-</code> | <code>n</code> </td><td> Push stack size.
88 </td></tr>
89 <tr>
90 <td> <code>dup</code> </td><td> <code>a</code> | <code>a a</code> </td><td> Duplicate top of stack.
91 </td></tr>
92 <tr>
93 <td> <code>swap</code> </td><td> <code>a b</code> | <code>b a</code> </td><td> Swap the two topmost stack values.
94 </td></tr>
95 <tr>
96 <td> <code>pop</code> </td><td> <code>a</code> | <code>-</code> </td><td> Remove top value from stack.
97 </td></tr>
98 <tr>
99 <td> <code>get</code> </td><td> <code>ptr</code> | <code>value</code> </td><td> Push value at pointer to stack (C <code>*ptr</code>).
100 </td></tr>
101 <tr>
102 <td> <code>put</code> </td><td> <code>ptr val</code> | <code>-</code> </td><td> Store value at pointer (C <code>*ptr = val</code>).
103 </td></tr>
104 <tr>
105 <td> <code>rnd</code> </td><td> <code>-</code> | <code>n</code> </td><td> Push random value.
106 </td></tr>
107 <tr>
108 <td> <code>&gt;</code> </td><td> <code>a b</code> | <code>(a&gt;b)</code> </td><td> Push 1 if a is greater than b, else 0.
109 </td></tr>
110 <tr>
111 <td> <code>&lt;</code> </td><td> <code>a b</code> | <code>(a&lt;b)</code> </td><td> Push 1 if a is less than b, else 0.
112 </td></tr>
113 <tr>
114 <td> <code>==</code> </td><td> undefined </td><td> Undefined.
115 </td></tr>
116 <tr>
117 <td> <code>.</code> </td><td> <code>n</code> | <code>-</code> </td><td> Pop a value, output as integer, adding a newline.
118 </td></tr>
119 <tr>
120 <td> <code>..</code> </td><td> <code>n</code> | <code>-</code> </td><td> Pop a value, output as an ASCII character.
121 </td></tr>
122 <tr>
123 <td> <code>mod</code> </td><td> <code>a b</code> | <code>(a%b)</code> </td><td> Modulo.
124 </td></tr>
125 <tr>
126 <td> <code>+</code> </td><td> <code>a b</code> | <code>(a+b)</code> </td><td> Addition.
127 </td></tr>
128 <tr>
129 <td> <code>-</code> </td><td> <code>a b</code> | <code>(a-b)</code> </td><td> Subtraction.
130 </td></tr>
131 <tr>
132 <td> <code>*</code> </td><td> <code>a b</code> | <code>(a*b)</code> </td><td> Multiplication.
133 </td></tr>
134 <tr>
135 <td> <code>/</code> </td><td> <code>a b</code> | <code>(a/b)</code> </td><td> Division, result is rounded towards 0.
136 </td></tr></table>
137
138 </body>
139 </html>
0 *a *b *c
1 0 =a 1 =b
2 : fib a b + =c c . b =a c =b c 100000 < @fib ;
3 1 . fib
0 : puts dup .. @puts ;
1 0 10 33 100 108 114 111 119 32 44 111 108 108 101 72 puts
0 rem array functions ;
1 : dim 2 * alloc ;
2 : idx 8 * + ;
3 rem equality ;
4 : eq2 pop 0 ;
5 : eq - 1 swap @eq2 ;
6 rem test each element in the array ;
7 : walkarr2 i 1 + =i i cursz < @walkarr1 ;
8 : walkarr1 curn arr i idx get mod 0 eq =fd fd 0 eq @walkarr2 ;
9 : walkarr 0 dup =i =fd walkarr1 ;
10 rem implementation of algorithm ;
11 : sieve2 arr cursz idx curn put curn . cursz 1 + =cursz ;
12 : sieve1 walkarr fd 0 eq @sieve2 curn 1 + =curn cursz maxsz < @sieve1 ;
13 : sieve *i *fd *curn *cursz 2 . arr 2 put 3 =curn 1 =cursz sieve1 ;
14 rem memory handling ;
15 : primes *arr *maxsz dup =maxsz dim =arr sieve arr free ;
16 rem change the number to change the amount of primes ;
17 25 primes
(No changes)
55 * - updated Jul 1997 Chris Pressey, fixed minor bugs
66 * - updated Jul 1998 Chris Pressey, fixed more minor bugs
77 * - and ANSI C-ized: now case sensitive
8 * - updated Jul 2010 Chris Pressey, buildability w/gcc & pcc
89 * Usage : maentw maentw-expressions executes and exits
910 * maentw goes into interactive mode
1011 * maentw <maentwrog-file runs file through maentw
1314
1415 #include <stdio.h>
1516 #include <stdlib.h>
16 #include <alloc.h>
1717 #include <string.h>
1818 #include <ctype.h>
1919
7070 void process(char *s);
7171 void procstr(char *s);
7272
73 char * strdup(char * s)
74 {
75 char * t;
76 t = (char *)malloc(strlen(s) * sizeof(char));
77 strcpy(t, s);
78 return t;
79 }
80
8173 int main(argc, argv)
8274 int argc;
8375 char **argv;
(No changes)
(No changes)