]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
Added port handling functions to runtime
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 14 Nov 2014 19:52:20 +0000 (14:52 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 14 Nov 2014 19:52:20 +0000 (14:52 -0500)
build.rb
source/runtime/ports.c [new file with mode: 0644]
source/runtime/sclpl.c [new file with mode: 0644]
source/runtime/sclpl.h

index d0c3a625620ffe61560130755e4de5fcf46f3636..74332802368f62a6c6f41a65af19e741f5cc4045 100755 (executable)
--- 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 (file)
index 0000000..b359b97
--- /dev/null
@@ -0,0 +1,54 @@
+#include "sclpl.h"
+#include <stdio.h>
+
+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 (file)
index 0000000..4c6325f
--- /dev/null
@@ -0,0 +1 @@
+#include "sclpl.h"
index ae1699a5f9d28b4fff9c9d08a4e830b9adba8e7b..3197be67b7ae38aa12ededf5829952e83ed73941 100644 (file)
@@ -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 */