spl

systems programming language
git clone http://frotz.net/git/spl.git
Log | Files | Refs | README | LICENSE

main.spl (1674B)


      1 // Copyright 2023, Brian Swetland <swetland@frotz.net>
      2 // Licensed under the Apache License, Version 2.0.
      3 
      4 var indent u32 = 0;
      5 
      6 fn print_indent(fd i32) {
      7 	var n u32 = 0;
      8 	while n < indent {
      9 		writes(fd, "   ");
     10 		n++;
     11 	}
     12 }
     13 
     14 fn _dump_ast_node(fd i32, node Ast) {
     15 	var kind AstKind = node.kind;
     16 
     17 	print_indent(fd);
     18 	writes(fd, ast_kind[kind]);
     19 	writes(fd, " ");
     20 	indent++;
     21 	if kind == AST_CONST {
     22 		writex(fd, node.ival);
     23 	} else if kind == AST_FUNC {
     24 		writes(fd, node.name.text);
     25 		writes(fd, "(");
     26 		var param Symbol = node.sym.type.list;
     27 		while param != nil {
     28 			writes(fd, param.name.text);
     29 			if param.next != nil {
     30 				writes(fd, ", ");
     31 			}
     32 			param = param.next;
     33 		}
     34 		writes(fd, ") ");
     35 		writes(fd, node.sym.type.of.name.text);
     36 	} else if kind == AST_STRING {
     37 		printstr(fd, node.name.text);
     38 	} else if kind == AST_SYMBOL {
     39 		writes(fd, node.name.text);
     40 	}
     41 	writes(fd, "\n");
     42 	
     43 	dump_ast_node(fd, node.left);
     44 	dump_ast_node(fd, node.right);
     45 	indent = indent - 1;
     46 }
     47 
     48 fn dump_ast_node(fd i32, node Ast) {
     49 	if node != nil {
     50 		while true {
     51 			_dump_ast_node(fd, node);
     52 			node = node.next;
     53 			if node == nil {
     54 				break;
     55 			}
     56 		}
     57 	}
     58 }
     59 
     60 fn dump_ast(node Ast) {
     61 	dump_ast_node(1, node);
     62 }
     63 
     64 fn start() i32 {
     65 	ctx_init();
     66 	parse_init();
     67 	ctx.filename = os_arg(0);
     68 
     69 	var n u32 = 1;
     70 	while n < os_arg_count() {
     71 		var arg str = os_arg(n);
     72 		if arg[0] == '-' {
     73 			error("unsupported option '", arg, "'");
     74 		}
     75 		ctx.fd_in = fd_open(arg);
     76 		if ctx.fd_in == -1 {
     77 			error("cannot open '", arg, "'");
     78 		}
     79 		ctx.linenumber = 1;
     80 		ctx.filename = arg;
     81 
     82 		scan();
     83 		next();
     84 		parse_program();
     85 
     86 		fd_close(ctx.fd_in);
     87 		ctx.fd_in = -1;
     88 
     89 		n++;
     90 	}
     91 
     92 	dump_ast(ctx.program);
     93 	return 0;
     94 }
     95