mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 07:33:01 +00:00 
			
		
		
		
	Begin stl
This commit is contained in:
		
							
								
								
									
										11
									
								
								parse.c
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								parse.c
									
									
									
									
									
								
							| @@ -437,6 +437,17 @@ int gst_parse_cstring(GstParser *p, const char *string) { | ||||
|     return bytesRead; | ||||
| } | ||||
|  | ||||
| /* Parse a gst string */ | ||||
| int gst_parse_string(GstParser *p, uint8_t *string) { | ||||
|     uint32_t i; | ||||
|     p->status = GST_PARSER_PENDING; | ||||
| 	for (i = 0; i < gst_string_length(string); ++i) { | ||||
|     	if (p->status != GST_PARSER_PENDING) break; | ||||
|         dispatch_char(p, string[i]); | ||||
| 	} | ||||
|     return i; | ||||
| } | ||||
|  | ||||
| /* Parser initialization (memory allocation) */ | ||||
| void gst_parser(GstParser *p, Gst *vm) { | ||||
|     p->vm = vm; | ||||
|   | ||||
							
								
								
									
										5
									
								
								parse.h
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								parse.h
									
									
									
									
									
								
							| @@ -30,7 +30,10 @@ struct GstParser { | ||||
| /* Initialize a parser */ | ||||
| void gst_parser(GstParser *p, Gst *vm); | ||||
|  | ||||
| /* Parse a c style string. Returns true if successful */ | ||||
| /* Parse a c style string. Returns number of bytes read */ | ||||
| int gst_parse_cstring(GstParser *p, const char *string); | ||||
|  | ||||
| /* Parse a gst string. Returns number of bytes read */ | ||||
| int gst_parse_string(GstParser *p, uint8_t *string); | ||||
|  | ||||
| #endif /* end of include guard: PARSE_H_ONYWMADW */ | ||||
|   | ||||
							
								
								
									
										75
									
								
								stl.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								stl.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,75 @@ | ||||
| /* This implemets a standard library in gst. Some of this | ||||
|  * will eventually be ported over to gst if possible */ | ||||
| #include "gst.h" | ||||
|  | ||||
| /****/ | ||||
| /* Misc */ | ||||
| /****/ | ||||
|  | ||||
| /* Print values for inspection */ | ||||
| int print(Gst *vm) { | ||||
|     uint32_t j, count; | ||||
|     count = gst_count_args(vm); | ||||
|     for (j = 0; j < count; ++j) { | ||||
|         string_put(stdout, gst_to_string(vm, gst_arg(vm, j))); | ||||
|         fputc('\n', stdout); | ||||
|     } | ||||
|     return GST_RETURN_OK; | ||||
| } | ||||
|  | ||||
| /****/ | ||||
| /* Parsing */ | ||||
| /****/ | ||||
|  | ||||
| /* Parse a source string into an AST */ | ||||
| int gst_stl_parse(Gst *vm) { | ||||
| 	uint8_t *source = gst_to_string(vm, gst_arg(vm, 0)); | ||||
| 	GstParser p; | ||||
|     /* init state */ | ||||
|     gst_parser(&p, vm); | ||||
|  | ||||
|     /* Get and parse input until we have a full form */ | ||||
|     gst_parse_string(&p, source); | ||||
| 	if (p.status == GST_PARSER_PENDING) { | ||||
| 		gst_c_throwc(vm, "incomplete source"); | ||||
| 	} else if (p.status == GST_PARSER_ERROR) { | ||||
|         gst_c_throwc(vm, p.error); | ||||
| 	} else { | ||||
| 		gst_c_return(vm, p.value); | ||||
|     } | ||||
| } | ||||
|  | ||||
| /****/ | ||||
| /* Compiling */ | ||||
| /****/ | ||||
|  | ||||
| /* Compile an ast */ | ||||
| int gst_stl_compile(Gst *vm) { | ||||
|     GstValue ast = gst_arg(vm, 0); | ||||
|     GstValue env = gst_arg(vm, 1); | ||||
|     GstValue ret; | ||||
|     GstCompiler c; | ||||
|     /* init state */ | ||||
|     gst_compiler(&c, vm); | ||||
|     /* Check for environment variables */ | ||||
| 	if (env.type == GST_OBJECT) { | ||||
| 		/* Iterate through environment, adding globals */ | ||||
| 	} else if (env.type != GST_NIL) { | ||||
| 		gst_c_throwc(vm, "invalid type for environment"); | ||||
| 	}         | ||||
| 	/* Prepare return value */ | ||||
| 	ret.type = GST_FUNCTION; | ||||
|     ret.data.function = gst_compiler_compile(&c, ast); | ||||
|     /* Check for errors */ | ||||
|     if (c.error != NULL) { | ||||
| 		gst_c_return(vm, ret); | ||||
|     } else { | ||||
| 		gst_c_throwc(vm, c.error); | ||||
|     } | ||||
| } | ||||
|  | ||||
| /****/ | ||||
| /* IO */ | ||||
| /****/ | ||||
|  | ||||
| /* TODO - add userdata to allow for manipulation of FILE pointers. */ | ||||
							
								
								
									
										10
									
								
								util.h
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								util.h
									
									
									
									
									
								
							| @@ -49,10 +49,16 @@ | ||||
| /* C function helpers */ | ||||
|  | ||||
| /* Return in a c function */ | ||||
| #define gst_c_return(vm, x) (do { (vm)->ret = (x); return GST_RETURN_OK; } while (0)) | ||||
| #define gst_c_return(vm, x) do { (vm)->ret = (x); return GST_RETURN_OK; } while (0) | ||||
|  | ||||
| /* Throw error from a c function */ | ||||
| #define gst_c_throw(vm, e) (do { (vm)->ret = (e); return GST_RETURN_ERROR; } while (0)) | ||||
| #define gst_c_throw(vm, e) do { (vm)->ret = (e); return GST_RETURN_ERROR; } while (0) | ||||
|  | ||||
| /* Throw c string error from a c function */ | ||||
| #define gst_c_throwc(vm, e) gst_c_throw((vm), gst_load_cstring((vm), (e))) | ||||
|  | ||||
| /* Assert from a c function */ | ||||
| #define gst_c_assert(vm, cond, e) do {if (cond) gst_c_throw((vm), (e)); } while (0) | ||||
|  | ||||
| /* What to do when out of memory */ | ||||
| #ifndef GST_OUT_OF_MEMORY | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Calvin Rose
					Calvin Rose