cc part 1

This commit is contained in:
Lisa Milne 2025-08-30 23:28:20 +10:00
parent 62d8fedc38
commit 219d7455dd
4 changed files with 149 additions and 9 deletions

140
clite/build.js Normal file
View file

@ -0,0 +1,140 @@
clite.commands.data = function() {
// insert commands below this line
clite.commands.load('cc',function(args,env,io) {
var stdlib = io.include('stdlib');
var stdio = io.include('stdio');
var time = io.include('time');
var clite = io.include('clite');
var ifiles = [];
var ofile = null;
var link = true;
var includes = [];
var code = [];
function help() {
stdio.printf(`
cc - code compiler
Usage: ls [OPTIONS] <FILE>
Options:
-? Print this help information
-o <FILE> Output to <FILE>
-c Compile but do not link
`);
}
function compile(file) {
var fd = stdio.open(file,stdio.flags.O_RDONLY);
if (!fd) {
stdio.fprintf(io.stderr,'Error: Cannot open file: %s\n',file);
return false;
}
var c = stdio.readAll(fd);
stdio.close(fd);
if (!c) {
stdio.fprintf(io.stderr,'Error: Cannot read file: %s\n',file);
return false;
}
try{
var f = new Function('args','env','io',c+';return main(args.length,args);');
} catch(e) {
var l = '';
if (typeof e.lineNumber !== 'undefined')
l = (e.lineNumber-2)+':';
stdio.fprintf(io.stderr,'Error: %s:%s %s\n',file,l,e.message);
return false;
}
code.push(c);
return true;
}
function output() {
if (!link) {
stdio.fprintf(io.stderr,'Error: Compile-only is not currently supported\n',ofile);
return false;
}
var c = code.join('\n')+';return main(args.length,args);';
var f;
try{
f = new Function('args','env','io',c);
} catch(e) {
stdio.fprintf(io.stderr,'Error: Could not create executable file: %s\n',e.message);
return false;
}
var fd = stdio.open(ofile,stdio.flags.O_WRONLY|stdio.flags.O_CREAT|stdio.flags.O_TRUNC);
if (!fd) {
stdio.fprintf(io.stderr,'Error: Cannot open output file: %s\n',ofile);
return false;
}
if (!stdio.write(fd,f)) {
stdio.close(fd);
stdio.fprintf(io.stderr,'Error: Cannot write output file: %s\n',ofile);
return false;
}
stdio.close(fd);
return true;
}
function main(argc,argv) {
for (var i=1; i<argc; i++) {
if (argv[i][0] == '-') {
for (var j=1; j<argv[i].length; j++) {
switch (argv[i][j]) {
case '?':
help();
return 0;
break;
case 'o':
ofile = true;
break;
case 'c':
link = false;
break;
default:
stdio.fprintf(io.stderr,'unknown argument: -%c\n',argv[i][j]);
}
}
}else if (ofile === true) {
ofile = clite.resolvePath(argv[i],false);
}else{
ifiles.push(clite.resolvePath(argv[i],false));
}
}
if (ofile == true)
return 1;
if (ifiles.length < 1) {
stdio.fprintf(io.stderr,'Error: No files specified\n');
return 1;
}
if (ofile == null)
ofile = clite.resolvePath('out.bin',false);
for (var i=0; i<ifiles.length; i++) {
if (!compile(ifiles[i]))
break;
}
if (!output())
return 1;
return 0;
}
return main(args.length,args);
});
// insert commands above this line
}

View file

@ -207,7 +207,7 @@ Options:
|| st.type == stdio.types.FT_DIR
|| st.type == stdio.types.FT_LINK
|| st.type == stdio.types.FT_BINARY
|| st.type == stdio.types.FT_UNKOWN
|| st.type == stdio.types.FT_UNKNOWN
|| st.type == stdio.types.FT_IMAGE
) {
if (st) {
@ -282,7 +282,7 @@ Options:
|| st.type == stdio.types.FT_DIR
|| st.type == stdio.types.FT_LINK
|| st.type == stdio.types.FT_BINARY
|| st.type == stdio.types.FT_UNKOWN
|| st.type == stdio.types.FT_UNKNOWN
|| st.type == stdio.types.FT_IMAGE
) {
stdio.fprintf(io.stderr,'not a normal file: %s\n',file);

View file

@ -8,7 +8,7 @@ var clite = {
},
includes:{
// a list of program/command files to load
prog:['commands.js','shell.js','vi.js','user.js'],
prog:['commands.js','shell.js','vi.js','user.js','build.js'],
// a list of library files to load
libs:['libclite.js','libcurses.js','libstd.js','libstdio.js','libterm.js','libtime.js','libauth.js']
},
@ -2229,7 +2229,7 @@ clite.io = {
// 8: image
// 9: shared object (library)
types:{
FT_UNKOWN:0,
FT_UNKNOWN:0,
FT_TEXT:1,
FT_BINARY:2,
FT_DIR:3,
@ -3220,7 +3220,7 @@ clite.lib = {
if (fd.node.data.content == null) {
if (fd.node.data.remote != null)
return clite.io.types.FT_REMOTE;
return clite.io.types.FT_UNKOWN;
return clite.io.types.FT_UNKNOWN;
}
if (fd.node.data.islink)
return clite.io.types.FT_LINK;
@ -3241,7 +3241,7 @@ clite.lib = {
}else if (fd.node.data.content.length == 3) {
return clite.io.types.FT_BINARY;
}else{
return clite.io.types.FT_UNKOWN;
return clite.io.types.FT_UNKNOWN;
}
break;
case 'object':
@ -3255,9 +3255,9 @@ clite.lib = {
case 'bigint':
case 'undefined':
default:
return clite.io.types.FT_UNKOWN;
return clite.io.types.FT_UNKNOWN;
}
return clite.io.types.FT_UNKOWN;
return clite.io.types.FT_UNKNOWN;
},
checkNodeReadable:function(node,pid) {
var uid = clite.proc.getUID(pid);

View file

@ -222,7 +222,7 @@ stdio (libio.so): io.include('stdio')
stdio.types:
object for mapping values of stat.type:
FT_UNKOWN: 0 Unknown file type
FT_UNKNOWN: 0 Unknown file type
FT_TEXT: 1 Plain text file
FT_BINARY: 2 Binary file, likely a javascript function
FT_DIR: 3 Directory