From: Michael D. Lowis Date: Fri, 14 Nov 2014 19:52:20 +0000 (-0500) Subject: Added port handling functions to runtime X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=a9589652387ad1bab6940dfe951bb89541dcd571;p=proto%2Fsclpl.git Added port handling functions to runtime --- diff --git a/build.rb b/build.rb index d0c3a62..7433280 100755 --- a/build.rb +++ b/build.rb @@ -15,14 +15,14 @@ base_env = BuildEnv.new do |env| env.build_dir('modules','build/obj/modules') env.add_builder :Install do |target, sources, cache, env, vars| - is_dir = (sources.length > 1) || Dir.exists?(sources.first) + is_dir = (sources.length > 1) || Dir.exists?(sources.first) || Dir.exists?(target) outdir = is_dir ? target : File.dirname(target) # Collect the list of files to copy over file_map = {} if is_dir sources.each do |src| if Dir.exists? src - Dir["#{src}/**/*"].each do |subfile| + Dir.glob("#{src}/**/*", File::FNM_DOTMATCH).each do |subfile| subpath = Pathname.new(subfile).relative_path_from(Pathname.new(src)).to_s file_map[subfile] = "#{outdir}/#{subpath}" end @@ -38,7 +38,7 @@ base_env = BuildEnv.new do |env| puts "INSTALL #{target}" file_map.each do |k,v| cache.mkdir_p(File.dirname(v)) - FileUtils.cp(k,v) + FileUtils.cp(k, v, :preserve => true) end cache.register_build(target, :Install, file_map.keys, env) end @@ -79,8 +79,9 @@ end #------------------------------------------------------------------------------ # Release Build Targets #------------------------------------------------------------------------------ -main_env.Library('build/lib/libcds.a', FileList['modules/libcds/source/**/*.c']) +main_env.Library('build/lib/libcds.a', FileList['modules/libcds/source/**/*.c']) main_env.Library('build/lib/libopts.a', FileList['modules/libopts/source/**/*.c']) +main_env.Library('build/lib/libsrt.a', FileList['source/runtime/*.c']) main_env.Program('build/bin/sclpl', FileList['source/sclpl/*.c', 'build/lib/libopts.a', 'build/lib/libcds.a']) main_env.Install('build/include/sclpl.h', 'source/runtime/sclpl.h') diff --git a/source/runtime/ports.c b/source/runtime/ports.c new file mode 100644 index 0000000..b359b97 --- /dev/null +++ b/source/runtime/ports.c @@ -0,0 +1,54 @@ +#include "sclpl.h" +#include + +static inline _Value make_port(FILE* file) { + return __struct(1, ((_Value)file | 1u)); +} + +static inline FILE* get_file(_Value port) { + return (FILE*)(__struct_fld(port,0) & ~1); +} + +_Value __port_read_char(_Value port) +{ + return __num(fgetc(get_file(port))); +} + +_Value __port_write_char(_Value port, _Value ch) +{ + fputc((int)__untag(ch), get_file(port)); + return __nil; +} + +_Value __port_read_byte(_Value port) +{ + return __num(fgetc(get_file(port))); +} + +_Value __port_write_byte(_Value port, _Value byte) +{ + fputc((int)__untag(byte), get_file(port)); + return __nil; +} + +_Value __open_input_file(_Value fname) +{ + return make_port(fopen((char*)fname, "r")); +} + +_Value __open_output_file(_Value fname) +{ + return make_port(fopen((char*)fname, "w")); +} + +_Value __close_port(_Value port) +{ + fclose(get_file(port)); + return __nil; +} + +_Value __is_eof(_Value port) +{ + return __bool(feof(get_file(port))); +} + diff --git a/source/runtime/sclpl.c b/source/runtime/sclpl.c new file mode 100644 index 0000000..4c6325f --- /dev/null +++ b/source/runtime/sclpl.c @@ -0,0 +1 @@ +#include "sclpl.h" diff --git a/source/runtime/sclpl.h b/source/runtime/sclpl.h index ae1699a..3197be6 100644 --- a/source/runtime/sclpl.h +++ b/source/runtime/sclpl.h @@ -223,4 +223,14 @@ typedef _Value (*__fnptr_n)(_Value env, ...); #define __substring(val, start, end) assert(false) #define __string_concat(lval, rval) assert(false) +/* Port Operations */ +_Value __port_read_char(_Value port); +_Value __port_write_char(_Value port, _Value ch); +_Value __port_read_byte(_Value port); +_Value __port_write_byte(_Value port, _Value byte); +_Value __open_input_file(_Value fname); +_Value __open_output_file(_Value fname); +_Value __close_port(_Value port); +_Value __is_eof(_Value port); + #endif /* SCLPL_H */