Initial Commit

This commit is contained in:
Armando Rivera 2013-11-06 20:08:19 -05:00
commit c3abc4f553
8 changed files with 479 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
demo
output.txt
*.exe
*.swp

63
Keywords.txt Normal file
View File

@ -0,0 +1,63 @@
_TO_
ADDRESS
AND
AS
BEGIN
BOOL
BYREF
CASE
CASE_ELSE
CLASS
CONSTANT
DECLARE
DECR
DIM
ELSE
ENC$
END
ENDCASE
ENDFUNCTION
ENDIF
ENDMAIN
ENDSELECT
ENDSUB
FALSE REVERSE$
FOR
FUNCTION
IF
INCR
INSTR
INT
LCASE$
LEFT$
LOADFILE$
LTRIM$
MAIN
MAP
MCASE$
MID$
NEXT
NOT
OR
PRIOR
REPLACE$
RETURN
RIGHT$
RTRIM$
SAVEFILE
SELECT
SPLITPATH$
STEP
STR$
SUB
THEN
TO
TRIM$
TRUE
TYPE
UCASE$
UINT
VAL \nPRINT
VECTOR
WEND
WHILE

17
Prototypes.txt Normal file
View File

@ -0,0 +1,17 @@
ENC$ (CSTRING A, INT L, INT R)
INSTR (CSTRING s,CSTRING match, size_t offset)
LCASE$ (CSTRING str)
LEFT$ (CSTRING s, INT length)
LOADFILE$ (CSTRING N)
LTRIM$ (CSTRING s)
MCASE$ (CSTRING S)
MID$ (CSTRING s, INT start, INT length)
REPLACE$ (CSTRING subject, CONSTANT CSTRING& search, CONSTANT CSTRING& replace)
REVERSE$ (CSTRING src)
RIGHT$ (CSTRING s, INT length)
RTRIM$ (CSTRING s)
SAVEFILE (CSTRING src, CSTRING fname)
SPLITPATH$ (CSTRING FPATH, INT mask)
TRIM$ (CSTRING s)
UCASE$ (CSTRING str)
VAL (CSTRING str) \nPRINT (CSTRING A="")

90
cppbas.inc Normal file
View File

@ -0,0 +1,90 @@
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <setjmp.h>
#include <time.h>
#include <stdarg.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <sys/wait.h>
#include <dlfcn.h>
#endif
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
/* DEFINES */
#define MAIN int main (int argc, char** argv) {
#define ENDMAIN }
#define DECLARE
#define FUNCTION
#define ENDFUNCTION }
#define DIM
#define AS
#define SUB void
#define ENDSUB }
#define BEGIN {
#define END }
#define AND &&
#define OR ||
#define CLASS class
#define TYPE typedef struct
#define ADDRESS &
#define INCR ++
#define DECR --
#define NEXT ++
#define PRIOR --
#define BYREF *
#define NOT not
#define IF if
#define THEN {
#define ELSE } else {
#define ENDIF }
#define FOR for
#define TO ;
#define STEP ;
#define SELECT switch
#define CASE case
#define _TO_ ...
#define ENDCASE break;
#define CASE_ELSE default
#define ENDSELECT }
#define WHILE while
#define WEND }
#define RETURN return
#define CONSTANT const
#define STR$( x ) dynamic_cast< std::ostringstream & >( ( std::ostringstream() << std::dec << x ) ).str()
#define BOOL bool
#define INT int
#define UINT unsigned int
#define VECTOR std::vector
#define MAP std::map
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* TYPEDEFS */
typedef std::string CSTRING;
char LF [2]= {10,0}; // Line Feed
#include "cppbasrt.inc"

204
cppbasrt.inc Normal file
View File

@ -0,0 +1,204 @@
DECLARE FUNCTION CSTRING LTRIM$ (CSTRING);
DECLARE FUNCTION CSTRING RTRIM$ (CSTRING);
DECLARE FUNCTION CSTRING TRIM$ (CSTRING);
DECLARE FUNCTION CSTRING LEFT$ (CSTRING, INT);
DECLARE FUNCTION CSTRING MID$ (CSTRING, INT, INT);
DECLARE FUNCTION CSTRING RIGHT$ (CSTRING, INT);
DECLARE FUNCTION INT INSTR (CSTRING,CSTRING,size_t=0);
DECLARE FUNCTION CSTRING LCASE$ (CSTRING);
DECLARE FUNCTION CSTRING UCASE$ (CSTRING);
DECLARE FUNCTION CSTRING MCASE$ (CSTRING);
DECLARE FUNCTION CSTRING LOADFILE$ (CSTRING);
DECLARE FUNCTION CSTRING SPLITPATH$ (CSTRING, INT);
DECLARE FUNCTION CSTRING ENC$ (CSTRING,INT=34,INT=34);
DECLARE FUNCTION CSTRING REVERSE$ (CSTRING);
DECLARE FUNCTION CSTRING REPLACE$ (CSTRING subject, CONSTANT CSTRING& search, CONSTANT CSTRING& replace);
DECLARE FUNCTION INT VAL (CSTRING);
DECLARE SUB PRINT (CSTRING);
DECLARE SUB SAVEFILE(CSTRING src, CSTRING fname);
FUNCTION CSTRING REVERSE$ (CSTRING src) BEGIN
RETURN CSTRING( src.rbegin(),src.rend() );
ENDFUNCTION
SUB PRINT (CSTRING A="") BEGIN
IF (A.empty()) THEN
std::cout << std::endl;
ELSE
std::cout << A << std::endl;
ENDIF
ENDSUB
FUNCTION CSTRING LTRIM$ (CSTRING s) BEGIN
s.erase(s.begin(),std::find_if(s.begin(),s.end(),std::not1(std::ptr_fun<INT,INT>(std::isspace))));
RETURN s;
ENDFUNCTION
FUNCTION CSTRING RTRIM$ (CSTRING s) BEGIN
s.erase(std::find_if(s.rbegin(),s.rend(),std::not1(std::ptr_fun<INT,INT>(std::isspace))).base(),s.end());
RETURN s;
ENDFUNCTION
FUNCTION CSTRING TRIM$ (CSTRING s) BEGIN
RETURN LTRIM$ (RTRIM$ (s));
ENDFUNCTION
FUNCTION CSTRING LEFT$ (CSTRING s, INT length) BEGIN
RETURN s.substr(0,length);
ENDFUNCTION
FUNCTION CSTRING MID$ (CSTRING s, INT start, INT length) BEGIN
RETURN s.substr(start,length);
ENDFUNCTION
FUNCTION CSTRING RIGHT$ (CSTRING s, INT length) BEGIN
RETURN s.substr(s.size()-length);
ENDFUNCTION
FUNCTION INT INSTR (CSTRING s,CSTRING match, size_t offset) BEGIN
IF (s.empty() OR match.empty() OR offset>s.length()) THEN
RETURN 0;
ENDIF
RETURN s.find(match,offset);
ENDFUNCTION
FUNCTION CSTRING LCASE$ (CSTRING str) BEGIN
DIM AS CSTRING name(str);
std::transform(name.begin(),name.end(),name.begin(),::tolower);
RETURN name;
ENDFUNCTION
FUNCTION CSTRING UCASE$ (CSTRING str) BEGIN
DIM AS CSTRING name(str);
std::transform(name.begin(),name.end(),name.begin(),::toupper);
RETURN name;
ENDFUNCTION
FUNCTION CSTRING MCASE$ (CSTRING S) BEGIN
DIM AS CSTRING tmpStr(S);
DIM AS bool capFlag;
DIM AS register size_t i;
std::transform(tmpStr.begin(),tmpStr.end(),tmpStr.begin(),::tolower);
FOR (i=0 TO i<=tmpStr.length() STEP i++) BEGIN
IF (std::ispunct(tmpStr[i]) OR std::isspace(tmpStr[i])) THEN
capFlag=FALSE;
ENDIF
IF (capFlag==FALSE AND std::isalpha(tmpStr[i])) THEN
tmpStr[i]=std::toupper(tmpStr[i]);
capFlag=TRUE;
ENDIF
END
RETURN tmpStr;
ENDFUNCTION
FUNCTION CSTRING LOADFILE$ (CSTRING N) BEGIN
DIM AS CSTRING line, tmpStr;
DIM AS std::ifstream myFile(N.c_str());
IF( NOT myFile.good()) BEGIN
PRINT("Error opening file");
RETURN "ERROR";
ENDIF
WHILE ( NOT myFile.eof()) BEGIN
getline(myFile,line);
line+=LF;
tmpStr+=line;
WEND
myFile.close();
RETURN tmpStr;
ENDFUNCTION
SUB SAVEFILE (CSTRING src, CSTRING fname) BEGIN
DIM AS std::ofstream outfile;
outfile.open(fname.c_str());
outfile << src;
outfile.close();
ENDSUB
FUNCTION CSTRING SPLITPATH$ (CSTRING FPATH, INT mask) BEGIN
DIM AS CSTRING fullPath(FPATH);
DIM AS CSTRING path, filename, extension;
DIM AS CSTRING::size_type lastSlashPos, extPos;
lastSlashPos=fullPath.find_last_of("/");
extPos=fullPath.find_last_of(".");
IF (lastSlashPos==CSTRING::npos) THEN
path.empty();
filename=fullPath;
ELSE
path=fullPath.substr(0,lastSlashPos);
filename=fullPath.substr(lastSlashPos+1,(extPos-lastSlashPos)-1);
ENDIF
IF (extPos==CSTRING::npos) THEN
extension.empty();
ELSE
extension=fullPath.substr(extPos+1,fullPath.size()-extPos-1);
ENDIF
SELECT (mask) BEGIN
CASE 4:
RETURN path;
ENDCASE
CASE 8:
RETURN filename;
ENDCASE
CASE 12:
RETURN path.append("/").append(filename);
ENDCASE
CASE 16:
RETURN extension;
ENDCASE
CASE_ELSE:
RETURN "";
ENDSELECT
ENDFUNCTION
FUNCTION CSTRING ENC$ (CSTRING A, INT L, INT R) BEGIN
DIM AS std::stringstream buf;
buf<<(char)L<<A<<(char)R;
RETURN buf.str();
ENDFUNCTION
FUNCTION CSTRING REPLACE$ (CSTRING subject, CONSTANT CSTRING& search, CONSTANT CSTRING& replace) BEGIN
DIM AS size_t pos = 0;
WHILE( ( pos = subject.find(search, pos) ) != CSTRING::npos) BEGIN
subject.replace(pos, search.length(), replace);
pos += replace.length();
END
RETURN subject;
ENDFUNCTION
FUNCTION INT VAL (CSTRING str) BEGIN
RETURN atoi(str.c_str());
ENDFUNCTION

76
demo.cpp Normal file
View File

@ -0,0 +1,76 @@
#include "cppbas.inc"
MAIN
DIM AS CSTRING mystring = "This is a string.";
PRINT ( "LEFT(mystring,4)" );
PRINT (LEFT$(mystring,4));
PRINT ();
PRINT ( "MID$(mystring,5,4)" );
PRINT (MID$(mystring,5,4));
PRINT ();
PRINT ( "RIGHT$(mystring,7)");
PRINT (RIGHT$(mystring,7));
PRINT ();
PRINT ( "INSTR(mystring,\"is\")");
PRINT (STR$(INSTR(mystring,"is")));
PRINT ();
PRINT ( "LCASE$(mystring)" );
PRINT (LCASE$(mystring));
PRINT ();
PRINT ( "UCASE$(mystring)" );
PRINT (UCASE$(mystring));
PRINT ();
PRINT ( "MCASE$(mystring)" );
PRINT (MCASE$(mystring));
PRINT ();
PRINT ( "LTRIM$, RTRIM$, TRIM$" );
PRINT (LTRIM$(" This should not have leading spaces "));
PRINT (RTRIM$(" This should not have any trailing spaces "));
PRINT (TRIM$(" This should not have any leading or trailing spaces "));
PRINT ();
PRINT ( "SPLITPATH$(/Users/riveraa/myfile.txt,4)" );
PRINT (SPLITPATH$("/Users/riveraa/myfile.txt",4));
PRINT ( "SPLITPATH$(/Users/riveraa/myfile.txt,8)" );
PRINT (SPLITPATH$("/Users/riveraa/myfile.txt",8));
PRINT ( "SPLITPATH$(/Users/riveraa/myfile.txt,12)" );
PRINT (SPLITPATH$("/Users/riveraa/myfile.txt",12));
PRINT ( "SPLITPATH$(/Users/riveraa/myfile.txt,16)" );
PRINT (SPLITPATH$("/Users/riveraa/myfile.txt",16));
PRINT ();
PRINT ( "REVERSE$(mystring)" );
PRINT (REVERSE$(mystring));
PRINT ();
PRINT ( "REPLACE$(mystring,\"string\", \"Number\"" );
PRINT ( REPLACE$(mystring,"string","Number") );
PRINT ();
PRINT ( "VAL(\"123\") + 10" );
PRINT ( STR$(VAL("123") + 10) );
PRINT ();
PRINT ( "LOADFILE$()" );
DIM AS CSTRING fileContents, fname = "demo.cpp";
fileContents = LOADFILE$(fname);
// PRINT ( "Printing contents of \"" + fname + "\"" );
// PRINT (fileContents);
PRINT ();
PRINT ( "SAVEFILE()" );
SAVEFILE(fileContents,"output.txt");
PRINT ( "Contents saved to 'output.txt'");
ENDMAIN

17
generate-keywords.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/sh
# KEYWORDS
KEYWORDS=$(ack '#define (\w+.|\w+)' cppbas.inc --output='$1')
KEYWORDS+=$(ack '^FUNCTION \w+ (\w+.|\w+)' cppbasrt.inc --output='$1')
KEYWORDS+='\n'$(ack '^SUB (\w+)' cppbasrt.inc --output='$1')
# FUNCTION/SUB PROTOTYPES
PROTO=$(ack '^FUNCTION \w+ (.+\))' cppbasrt.inc --output='$1')' '
PROTO+='\n'$(ack '^SUB (.+\))' cppbasrt.inc --output='$1')
#
# SAVE SORTED KEYWORDS/PROTOTYPES
echo "$KEYWORDS" | sort > Keywords.txt
echo "$PROTO" | sort > Prototypes.txt
echo "Done."

7
readme.txt Normal file
View File

@ -0,0 +1,7 @@
Compile with: g++ demo.cpp -o demo (or if using clang, use clang++ demo.cpp -o demo)
This is just a proof of concept of using a BASIC-like syntax to program C++.
For the list of current Keywords, see the Keywords.txt file
For the Function/Sub prototypes, see the Prototypes.txt file