summaryrefslogtreecommitdiff
path: root/cl-forth.lisp
diff options
context:
space:
mode:
authormRnea <[email protected]>2024-08-09 11:41:14 +0300
committermRnea <[email protected]>2024-08-09 11:41:14 +0300
commitb574944656e3a0fa469a728ec7ed4483befb73de (patch)
tree7099c19d9fdeff4e6a23d7a68ff0fc811a6b3c03 /cl-forth.lisp
parent1056b74b115f2a0a5cdb6b05fffc6eb476fb1f3c (diff)
added ops for C codegen
Diffstat (limited to 'cl-forth.lisp')
-rw-r--r--cl-forth.lisp38
1 files changed, 8 insertions, 30 deletions
diff --git a/cl-forth.lisp b/cl-forth.lisp
index 66327e5..88019ce 100644
--- a/cl-forth.lisp
+++ b/cl-forth.lisp
@@ -188,35 +188,11 @@
(defmethod write-program ((target (eql :c)) program out &key (mem-cap 640000))
(declare (ignore mem-cap))
- (format out
- "#include <stdio.h>
-
-struct Stack {
- int content[100];
- int i;
-};
-
-typedef struct Stack Stack;
-
-void push(Stack* stack, int val){
- stack->content[stack->i] = val;
- stack->i += 1;
-}
-
-int pop(Stack* stack){
- stack->i -= 1;
- return stack->content[stack->i];
-}
-
-Stack stack;
-int rax, rbx;
-
-int main(void){
- stack.i = 0;
-")
- (iter (for op in-sequence program)
- (write-op target out (car op) (cdr op)))
- (format out " return 0;~%}~%"))
+ (gen-c-stack out)
+ (with-c-fn (:int main) () out
+ (iter (for op in-sequence program)
+ (write-op target out (car op) (cdr op)))
+ (format out "~% return 0;~%")))
(defun generate-program (program
&key (path "output.asm") (compile nil)
@@ -238,7 +214,9 @@ int main(void){
:output t :silence silence)))
(defmethod compile-program ((target (eql :c)) path silence)
- (run `("gcc" ,path) :output t :silence silence))
+ (let ((name (first (uiop:split-string path :separator '(#\.)))))
+ (run `("gcc" ,path "-o" ,name)
+ :output t :silence silence)))