Harigami
ログイン
1
anonymous タイトルなし
Go
package main
import (
	"fmt"
	"strings"
	"os"
)
func main() {
	args := os.Args
	vt := var_type()
	vtype := strings.Split(vt, ",")
	support_vtype := []string{"int", "char", "double"}
	if len(args) == 2 {
		filename := args[1]
		file, err := os.Open(filename)
		if err != nil{
			fmt.Printf("No Such file : %s", filename)
			os.Exit(0)
		}
		nfilename,ext := get_ext_filename(filename)
		if ext != "sl"{
			fmt.Println(filename, "is not SasaLan File")
			os.Exit(0)
		}
		buf := make([]byte, 1024)
		var add_code string
		for {
			n,err := file.Read(buf)
			if n == 0{
				break
			}
			if err != nil{
				fmt.Println("Read Error")
				break
			}
			code := string(buf[:n])
			add_code += code
		}
		nl_split := strings.Split(add_code, "\n")
		size := len(nl_split)
		var var_string string
		var var_type_s string
		in_if, in_func := "0", "0"
		var func_code string
		var main_code string
		main_code += "#include <stdio.h>\nint main(void){\n"
		for i := 0;i < size;i++{
			code := nl_split[i]
			space_split := strings.Split(code, " ")
			if code == ""{
				continue
			}
			if string(code[0]) == "#" {
				var comment_value string
				for i := 0;i < len(code);i++ {
					if i == 0{
						continue
					} else {
						comment_value += string(code[i])
					}
				}
				add_code := "/*" + comment_value + "*/\n"
				main_code += add_code
			} else if len(space_split) >= 2 {
				if space_split[0] == "if" {
					var judge_formula string
					for i,value := range space_split {
						if i == 0 {
							continue
						} else if i == 1 {
							judge_formula += value
						} else {
							judge_formula += " " + value
						}
					}
					add_code := if_code(judge_formula)
					in_if += ",1"
					main_code += add_code
				}
			} else if code == "e" {
				if_sp, func_sp := strings.Split(in_if, ","), strings.Split(in_func, ",")
				lengh1, lengh2 := len(if_sp) - 1, len(func_sp) - 1
				ires, fres := if_sp[lengh1], func_sp[lengh2]
				if ires == "1" {
					main_code += "}\n"
				} else if fres == "1" {
					func_code += "}"
				}
			}
			if space_split[0] == "pr" {
				var pr_value string
				for i := 1;i < len(code);i++{
					if i == 0 || i == 1 || i == 2{
						continue
					} else {
						pr_value += string(code[i])
					}
				}
				add_code := pr_code(pr_value, strings.Split(var_string, ","), strings.Split(var_type_s, ","))
				main_code += add_code
			}
			var_split1 := strings.Split(code, string(vtype[0]))
			var_split2 := strings.Split(code, string(vt[1]))
			var_split3 := strings.Split(code, "f:")
			if var_split1[0] == "" {
				var_name_value := var_split1[1]
				line_split1 := strings.Split(var_name_value, "=")
				var_name,var_value := line_split1[0], line_split1[1]
				lengh1, lengh2 := len(strings.Split(var_name, " ")), len(strings.Split(var_value, " "))
				var vname string
				var vvalue string
				if lengh1 == 2 && lengh2 == 2{
					var_name, var_value := strings.Split(var_name, " ")[0], strings.Split(var_value, " ")[1]
					vname += var_name
					vvalue += var_value
				}
				var_type := support_vtype[0]
				add_code := var_code(var_type, vname, vvalue)
				if len(var_string) == 0{
					var_string += vname
				} else {
					var_string += "," + vname
				}
				if len(var_type_s) == 0{
					var_type_s += var_type
				} else {
					var_type_s += "," + var_type
				}
				main_code += add_code
			} else if var_split2[0] == "s" {
				var_name_value := var_split2[1]
				line_split1 := strings.Split(var_name_value, "=")
				var_name,var_value := line_split1[0], line_split1[1]
				lengh1, lengh2 := len(strings.Split(var_name, " ")), len(strings.Split(var_value, " "))
				var vname string
				var vvalue string
				if lengh1 >= 2 && lengh2 >= 2 {
					var_name := strings.Split(var_name, " ")[0]
					var_value_sp := strings.Split(var_value, " ")
					var res string
					for i := 0;i < len(var_value_sp);i++ {
						if i == 0{
							continue
						} else if i == 1{
							res += var_value_sp[i]
						} else {
							res += " " + var_value_sp[i]
						}
					}
					vvalue += res
					vname += var_name
				} else {
					vname += var_name
					vvalue += var_value
				}
				var_type := support_vtype[1]
				add_code := var_code(var_type, vname, vvalue)
				if len(var_string) == 0{
					var_string += vname
				} else {
					var_string += "," + vname
				}
				if len(var_type_s) == 0{
					var_type_s += var_type
				} else {
					var_type_s += "," + var_type
				}
				main_code += add_code
			} else if var_split3[0] == ""{ //float && コード変換
				var_name_value := var_split3[1]
				line_split1 := strings.Split(var_name_value, "=")
				var_name, var_value := line_split1[0], line_split1[1]
				lengh1, lengh2 := len(strings.Split(var_name, " ")), len(strings.Split(var_value, " "))
				var vname string
				var vvalue string
				if lengh1 >= 2 && lengh2 >= 2 {
					var_name := strings.Split(var_name, " ")[0]
					var_value_sp := strings.Split(var_value, " ")
					var res string
					for i,value := range var_value_sp {
						if i == 0 {
							continue
						} else if i == 1 {
							res += value
						} else {
							res += " " + value
						}
					}
					vname += var_name
					vvalue += res
				} else {
					vname += var_name
					vvalue += var_value
				}
				var_type := support_vtype[2]
				add_code := var_code(var_type, vname, vvalue)
				if len(var_string) == 0{
					var_string += vname
				} else {
					var_string += "," + vname
				}
				if len(var_type_s) == 0{
					var_type_s += var_type
				} else {
					var_type_s += "," + var_type
				}
				main_code += add_code
			}
		}
		if string(main_code[len(main_code) - 1]) !="}" {
			main_code += "}"
		}
		cfilename := nfilename + ".c"
		nfile,err := os.Create(cfilename)
		if err != nil{
			fmt.Println("Write Error")
			os.Exit(0)
		}
		nfile.Write(([]byte)(main_code))
		fmt.Printf("Output C File : %s\n", cfilename)
	} else {
		fmt.Println("sasalan [filename]")
		os.Exit(0)
	}
}
func get_ext_filename(filename string) (string, string) {
	split_arr := strings.Split(filename, ".")
	max_lengh := len(split_arr) - 1
	ext := split_arr[max_lengh]
	return split_arr[0],ext
}
func var_type() string {
	filename := "doc/var_type"
	var code string
	file,err := os.Open(filename)
	if err != nil{
		fmt.Println("Error")
	}
	buf := make([]byte, 1024)
	for {
		n,err := file.Read(buf)
		if n == 0 {
			break
		}
		if err != nil{
			fmt.Println("Read Error")
		}
		ncode := string(buf[:n])
		code += ncode
	}
	tl := strings.Split(code, "\n")[0]
	return tl
}
func var_code(var_type string, var_name string, var_value string) string {
	var res string
	if var_type == "int" {
		code := fmt.Sprintf("int %s = %s;\n", var_name, var_value)
		res += code
	} else if var_type == "char"{
		code := fmt.Sprintf("char %s[] = %s;\n", var_name, var_value)
		res += code
	} else if var_type == "double" {
		code := fmt.Sprintf("float %s = %sf;\n", var_name, var_value)
		res += code
	}
	return res
}
func pr_code(put_string string, var_array []string, var_type_array []string) string {
	var judge1 rune = '"'
	var res string
	pr_split1 := strings.Split(put_string, string(judge1))
	join_split1 := strings.Split(put_string, ",")
	if len(pr_split1) == 3 {
		res += fmt.Sprintf("printf(%s);\n", put_string)
		return res
	}
	if len(join_split1) >= 2 {
		base := "printf(" + string(judge1)
		var after_str string
		judge2 := "0"
		for _,v := range join_split1 {
			fmt.Println(v)
			if string(v[0]) == string(judge1) {
				judge2 += ",0"
				if len(after_str) == 0 {
					after_str += v
				} else {
					after_str += "," + v
				}
				base += "%s"
				continue
			} else if string(v[len(v) - 1]) == string(judge1) {
				judge2 += ",1"
				if len(after_str) == 0 {
					after_str += v
				} else {
					after_str += "," + v
				}
			} else if strings.Split(judge2, ",")[len(strings.Split(judge2, ",")) - 1] == "1" {
				if len(after_str) == 0 {
					after_str += v
				} else {
					after_str += "," + v
				}
			} else {
				if len(after_str) == 0 {
					after_str += v
				} else {
					after_str += "," + v
				}
				var vtype string
				var res2 string
				for i := 0;i < len(var_array);i++ {
					if var_array[i] == v {
						res2 += "comp"
						vtype += var_type_array[i]
						break
					} else {
						res2 += "not"
					}
				}
				if res2 == "not" {
					fmt.Printf("There is not variable %s", v)
					os.Exit(0)
				}
				if vtype == "int" {
					base += "%d"
				} else if vtype == "char" {
					base += "%s"
				} else if vtype == "double" {
					base += "%f"
				}
				continue
			}
		}
		base += string(judge1) + ", " + after_str + ");\n"
		return base
	}
	var res2 string
	var vtype string
	for i := 0;i < len(var_array);i++ {
		if var_array[i] == put_string{
			res2 += "comp"
			vtype += var_type_array[i]
			break
		} else {
			res2 += "not"
		}
	}
	if res2 == "not" {
		fmt.Printf("There is not variable %s", put_string)
		os.Exit(0)
	} 
	if vtype == "int" {
		 code := "printf(" + string(judge1) + "%d" + string(judge1) + ", " + put_string + ");\n"
		 res += code
	} else if vtype == "char" {
		code := "printf(" + string(judge1) + "%s" + string(judge1) + ", " + put_string + ");\n"
		res += code
	} else if vtype == "double" {
		code := "printf(" + string(judge1) + "%f" + string(judge1) + ", " + put_string + ");\n"
		res += code
	}
	return res
}
func if_code(judge_formula string) string {
	return fmt.Sprintf("if (%s) {\n", judge_formula)
}
  • 0
  • 0
anonymous タイトルなし
Go
💩
  • 0
  • 0
あなたもコードを投稿しませんか?
投稿する
1