Skip to content
This repository has been archived by the owner on Nov 24, 2024. It is now read-only.

Commit

Permalink
1st swallow program working
Browse files Browse the repository at this point in the history
  • Loading branch information
SaptakBhoumik committed Sep 13, 2021
1 parent b6d3136 commit 6e2b3f9
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 64 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/push.bat
/push.sh
.idea
**/a.out
**/a.out
**/temp.c
56 changes: 7 additions & 49 deletions swallow/builtin.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Original author: MD Gaziur Rahman Noor

#ifndef SWALLOW_BUILTIN_H
#define SWALLOW_BUILTIN_H
// #ifndef SWALLOW_BUILTIN_H
// #define SWALLOW_BUILTIN_H
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -83,10 +83,9 @@ char *_format(const char *fmt, ...) {
/* _colorprint() implementation */

/*
* int16_t
* int64_t
*
* Put foreground color in first half
* and background color in second half
*/

// Foreground(text) colors
Expand All @@ -107,23 +106,6 @@ char *_format(const char *fmt, ...) {
#define SH_FG_BRIGHT_CYAN (36 << 8)
#define SH_FG_BRIGHT_WHITE (37 << 8)

// Background(text) colors
#define SH_BG_BLACK 40
#define SH_BG_RED 41
#define SH_BG_GREEN 42
#define SH_BG_YELLOW 43
#define SH_BG_BLUE 44
#define SH_BG_MAGENTA 45
#define SH_BG_CYAN 46
#define SH_BG_WHITE 47
#define SH_BG_BRIGHT_BLACK 100
#define SH_BG_BRIGHT_RED 101
#define SH_BG_BRIGHT_GREEN 102
#define SH_BG_BRIGHT_YELLOW 103
#define SH_BG_BRIGHT_BLUE 104
#define SH_BG_BRIGHT_MAGENTA 105
#define SH_BG_BRIGHT_CYAN 106
#define SH_BG_BRIGHT_WHITE 107

/*
* Builtin Color Print Function
Expand All @@ -133,36 +115,12 @@ char *_format(const char *fmt, ...) {
* specified when printed to stdout
*
* Example:
* _colorprint("Hello World!", SH_FG_BLACK | SH_BG_WHITE, true);
* _colorprint("Hello World!", SH_FG_BLACK , true);
*/
void _colorprint(const char *str, int16_t flags, bool reset) {
int8_t background = flags & 0x00FF;
void _colorprint(const char *str, int64_t flags, bool reset) {
int8_t foreground = (flags & 0xFF00) >> 8;

if(background == 0) {
// don't include background
printf("\e[1;%dm%s", foreground, str);
}
else {
printf("\e[1;%dm\e[1;%dm%s", foreground, background, str);
}

printf("\e[1;%dm%s", foreground, str);
if(reset)
printf("\e[1;0m");
}

/*
* Wrapper around _colorprint function which adds newline at the end
* Author: MD Gaziur Rahman Noor
*
* Works same as _colorprint() but adds newline at the end
*
* Example:
* _colorprintln("Hello World!", SH_FG_BLACK | SH_BG_WHITE);
*/
void _colorprintln(const char *str, int16_t flags) {
char *formatted_string = _format("{}\e[1;0m\n", str, False);
_colorprint(formatted_string, flags, False);
free(formatted_string);
}
#endif //SWALLOW_BUILTIN_H
// #endif //SWALLOW_BUILTIN_H
2 changes: 2 additions & 0 deletions swallow/builtin.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
#Builtin.h is imported by default
def print(str query):
Ccode printf("%s",query); Ccode
def colorprint(str string, int flags):
Ccode _colorprint(string,flags,True); Ccode
62 changes: 59 additions & 3 deletions swallow/codegen/codegen.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@ import parser

pub fn codegen(ast parser.Ast) []string{
required_arg:=["str_variable_required_argument","int_variable_required_argument","bool_variable_required_argument","list_variable_required_argument","dictionary_variable_required_argument","float_variable_required_argument","void_variable_required_argument"]
ast_type:=["function_call","function_define"]
mut is_function_call:=false
mut keyword:=""
mut first_bracket_count:=0
mut code:=[]string{}
mut previous_code_block:=parser.Body{}
for item in ast.body{
mut next_item:=parser.Body{}
for index,item in ast.body{
keyword=item.keyword
// println(is_function_call)
if index<ast.body.len-1 && index!=0{
next_item=ast.body[index+1]
}
if previous_code_block.ast_type=="function_define" && item.direction!="right"{
code<<"(){\n"
}
Expand All @@ -31,9 +41,55 @@ pub fn codegen(ast parser.Ast) []string{
if item.ast_type=="str_variable_required_argument"{
code<<"char * ${item.keyword}"
}
else if item.ast_type=="void_variable_required_argument"{
code<<"void * ${item.keyword}"
}
else if item.ast_type=="int_variable_required_argument"{
code<<"int64_t ${item.keyword}"
}
else if item.ast_type=="bool_variable_required_argument"{
code<<"bool ${item.keyword}"
}
}
else if item.ast_type =="function_call" && is_function_call==false{
code<<item.keyword
}
else if previous_code_block.ast_type =="function_call" && is_function_call==false{
code[code.len-1]+=keyword
}
else if is_function_call==true{
code[code.len-1]+=keyword
}
if item.keyword=="(" && is_function_call==true{
first_bracket_count++
}
else if item.keyword=="(" && is_function_call==false && previous_code_block.ast_type=="function_call"{
first_bracket_count++
}
else if item.keyword==")" && is_function_call==true{
first_bracket_count--
}
if first_bracket_count==0{
if is_function_call==true{
if next_item.line!=item.line || next_item==item || next_item.keyword==r"\n"{
code<<";\n"
}
}
is_function_call=false
}
else{
is_function_call=true
}
if previous_code_block.tab<item.tab && keyword!=r"\n" && is_function_call==false && (previous_code_block.ast_type in ast_type) == false{
code<<"\n}\n"
}
else if item==next_item && item!=parser.Body{}{
code<<"\n}\n"
}

if keyword!=r"\n"{
previous_code_block=item
}
previous_code_block=item
}
println(ast)
return code
}
6 changes: 5 additions & 1 deletion swallow/example.sw
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
def main():
print("First swallow program")
print("First swallow program\n")
print("First swallow program\n")
colorprint("First swallow program\n", SH_FG_GREEN)
colorprint("It can also print in other color but i am too lazy to show\n", SH_FG_BLUE)

5 changes: 2 additions & 3 deletions swallow/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn parser(code []string) (Ast,string){
}
}

else if is_argument==true && item!="(" && item!=")" && item!="," && (item in reserved_keywords)==false{
else if is_argument==true && item!="(" && item!=r"\n" && item!=")" && item!="," && item!=" " && (item in reserved_keywords)==false && next_item!="(" && is_function_call==false{
previus_item=item
code_block=Body{ast_type:"required_argument"
keyword : item
Expand All @@ -123,7 +123,7 @@ pub fn parser(code []string) (Ast,string){
json.body<<code_block
code_block=Body{}
}
else if is_argument==true && item==")"{
else if is_argument==true && item==")" && is_function_call==false{
is_argument=false
previus_item=item
}
Expand Down Expand Up @@ -358,7 +358,6 @@ pub fn parser(code []string) (Ast,string){
}

else if is_func_def==true && item!=" " && is_function_call==false{
println(item)

if item!=","{
code_block,previus_item,right=function(item,is_func_def,previus_item,json,right,tab)
Expand Down
Binary file added swallow/swallow
Binary file not shown.
15 changes: 13 additions & 2 deletions swallow/swallow.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import codegen
// import preprocessor
// Original author: Saptak Bhoumik
fn main() {
filename := "./example.sw"
arg:=os.args.clone()
filename := arg[2]
content := os.read_file(filename) ?
mut path:=os.executable()
path=path.replace(r"\","/")
Expand All @@ -19,8 +20,18 @@ fn main() {
builtin_h:= os.read_file("${folder}builtin.h")?
mut total:="${builtin_sw}\n${content}"
ast,error:=parser.parser(tokenizer.process_tokens(tokenizer.tokenize(total)))
mut final_code:=""
if error==""{
println(codegen.codegen(ast))
codes:=codegen.codegen(ast)
for code in codes{
final_code="$final_code$code"
}
final_code="//Generated using the swallow compiler \n $builtin_h \n $final_code"
mut x:=os.create("temp.c")?
x.writeln(final_code)?
x.close()
os.system("gcc ./temp.c -o ${arg.last()}")
os.system("rm ./temp.c")
}
else{
print("\033[0;31m")
Expand Down
5 changes: 0 additions & 5 deletions tests/builtin/colorprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
#include "../../swallow/builtin.h"

int main() {
_colorprintln("Hello World!", SH_BG_RED);
_colorprintln("Text Color Red", SH_FG_RED);
_colorprintln("Black text on white bg", SH_FG_BLACK | SH_BG_WHITE);

_colorprint("This ", SH_FG_GREEN, True);
_colorprint("is ", SH_FG_BLUE, True);
_colorprintln("colorprintln()", SH_FG_BRIGHT_MAGENTA);
return 0;
}

0 comments on commit 6e2b3f9

Please sign in to comment.