Advertisement
Ramaraunt1

Untitled

May 27th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. package ramscript
  2.  
  3. import (
  4. "strconv"
  5. "errors"
  6. )
  7.  
  8. /* SLICING */
  9. // Sliciing is the practice of cutting the code up into individual expressions.
  10. func slice(data string) ([]string, error) {
  11. sliced := make([]string,0,0)
  12. curSlice := ""
  13. inString := false
  14. for x := 0; x < len(data); x ++{
  15. if !inString{
  16. if (string(data[x]) == "\""){
  17. curSlice, sliced = clearSliced(curSlice, sliced)
  18. inString = true
  19. curSlice += string(data[x])
  20. } else if string(data[x]) == " " || string(data[x]) == "\n"{
  21. curSlice, sliced = clearSliced(curSlice, sliced)
  22. } else if checkForDoubleSymbols("=","=",data,x){
  23. sliced, curSlice, x = appendDoubleSymbols("=", "=", data, x, sliced, curSlice)
  24. } else if checkForDoubleSymbols("!","=",data,x){
  25. sliced, curSlice, x = appendDoubleSymbols("!", "=", data, x, sliced, curSlice)
  26. } else if checkForDoubleSymbols("<","=",data,x){
  27. sliced, curSlice, x = appendDoubleSymbols("<", "=", data, x, sliced, curSlice)
  28. } else if checkForDoubleSymbols(">","=",data,x){
  29. sliced, curSlice, x = appendDoubleSymbols(">", "=", data, x, sliced, curSlice)
  30. } else if checkForDoubleSymbols("&","&",data,x){
  31. sliced, curSlice, x = appendDoubleSymbols("&", "&", data, x, sliced, curSlice)
  32. } else if checkForDoubleSymbols("^","^",data,x){
  33. sliced, curSlice, x = appendDoubleSymbols("^", "^", data, x, sliced, curSlice)
  34. } else if checkForDoubleSymbols("|","|",data,x){
  35. sliced, curSlice, x = appendDoubleSymbols("|", "|", data, x, sliced, curSlice)
  36. } else if checkForDoubleSymbols("!","!",data,x){
  37. sliced, curSlice, x = appendDoubleSymbols("!", "!", data, x, sliced, curSlice)
  38. } else if checkForDoubleSymbols("+","=",data,x){
  39. sliced, curSlice, x = appendDoubleSymbols("+", "=", data, x, sliced, curSlice)
  40. } else if checkForDoubleSymbols("-","=",data,x){
  41. sliced, curSlice, x = appendDoubleSymbols("-", "=", data, x, sliced, curSlice)
  42. } else if checkForDoubleSymbols("*","=",data,x){
  43. sliced, curSlice, x = appendDoubleSymbols("*", "=", data, x, sliced, curSlice)
  44. } else if checkForDoubleSymbols("/","=",data,x){
  45. sliced, curSlice, x = appendDoubleSymbols("/", "=", data, x, sliced, curSlice)
  46. } else if checkForDoubleSymbols("%","=",data,x){
  47. sliced, curSlice, x = appendDoubleSymbols("%", "=", data, x, sliced, curSlice)
  48. } else if checkForDoubleSymbols("^","=",data,x){
  49. sliced, curSlice, x = appendDoubleSymbols("^", "=", data, x, sliced, curSlice)
  50. } else if checkForDoubleSymbols("@","=",data,x){
  51. sliced, curSlice, x = appendDoubleSymbols("@", "=", data, x, sliced, curSlice)
  52. } else if string(data[x]) == "(" || string(data[x]) == ")" || string(data[x]) == "," || string(data[x]) == "[" || string(data[x]) == "]" || string(data[x]) == "{" || string(data[x]) == "}" || string(data[x]) == ";" || string(data[x]) == ":" || string(data[x]) == "+" || string(data[x]) == "-" || string(data[x]) == "*" || string(data[x]) == "/" || string(data[x]) == "^" || string(data[x]) == "@" || string(data[x]) == "%" || string(data[x]) == "&" || (string(data[x]) == "." && x < len(string(data)) && !isNum(string(data[x+1]))){
  53. curSlice, sliced = clearSliced(curSlice, sliced)
  54. sliced = append(sliced, string(data[x]))
  55. } else {
  56. curSlice += string(data[x])
  57. }
  58. }else/*if inString*/ {
  59. curSlice += string(data[x])
  60. if string(data[x-1]) != "\\" && string(data[x]) == "\"" {
  61. curSlice, sliced = clearSliced(curSlice, sliced)
  62. inString = false
  63. }
  64. }
  65.  
  66. }
  67. if !inString{
  68. return sliced, nil
  69. } else/*if inString*/{
  70. return sliced, errors.New("[SYNTAX] Your quote marks are invalid.")
  71. }
  72. }
  73.  
  74. func checkForDoubleSymbols(symbol1 string, symbol2 string, data string, position int) bool {
  75. if position+1 <= len(string(data)) - 1 && string(data[position]) == symbol1 && string(data[position + 1]) == symbol2 {
  76. return true
  77. } else {
  78. return false
  79. }
  80. }
  81.  
  82. func appendDoubleSymbols(symbol1 string, symbol2 string, data string, position int, sliced []string, curSlice string) ([]string, string, int) {
  83. curSlice, sliced = clearSliced(curSlice, sliced)
  84. sliced = append(sliced, "==")
  85. position ++;
  86. return sliced, curSlice, position
  87. }
  88.  
  89. func clearSliced(curSlice string, sliced []string) (string, []string){
  90. if curSlice != "" {
  91. sliced = append(sliced, curSlice)
  92. }
  93. curSlice = ""
  94. return curSlice, sliced
  95. }
  96.  
  97. func isNum(value string) bool {
  98. if _, err := strconv.Atoi(value); err == nil {
  99. return true
  100. }else{
  101. return false
  102. }
  103. }
  104.  
  105. /* COMMON EXCEPTION CHECK*/
  106. // This is done to check the layout of the slices, and make sure they make sense.
  107. func commonExceptionCheck(data []string) error {
  108. //matching squiglys, squares, and parenthesis
  109. if true {
  110. var parenInset int = 0
  111. var squiglyInset int = 0
  112. var squareInset int = 0
  113. for x := 0; x < len(data); x ++ {
  114. if string(data[x]) == "["{
  115. squareInset++
  116. } else if string(data[x]) == "]"{
  117. squareInset--
  118. } else if string(data[x]) == "{" {
  119. squiglyInset++
  120. } else if string(data[x]) == "}" {
  121. squiglyInset--
  122. } else if string(data[x]) == "(" {
  123. parenInset++
  124. } else if string(data[x]) == ")" {
  125. parenInset--
  126. }
  127. }
  128.  
  129. if parenInset != 0 {
  130. return errors.New("[SYNTAX] Your parenthesis don't match up.")
  131. } else if squiglyInset != 0 {
  132. return errors.New("[SYNTAX] Your squigly brackets don't match up.")
  133. } else if squareInset != 0 {
  134. return errors.New("[SYNTAX] Your square brackets don't match up.")
  135. }
  136. }
  137. //reasonable statement size
  138. if true {
  139. prevEndl := 0
  140. for x := 0; x < len(data); x ++ {
  141. if string(data[x]) == ";" {
  142. if x - prevEndl > 3 {
  143. prevEndl = x
  144. } else {
  145. return errors.New("[SYNTAX] One of your statements has an invalid length.")
  146. }
  147. }
  148. }
  149. }
  150. //reasonable operator placement
  151. for x := 0; x < len(data); x ++ {
  152. if x < len(data) - 1 && isOperator(string(data[x])) && isOperatorNotIncludingMinus(string(data[x+1])) {
  153. return errors.New("[SYNTAX] One of your operators is placed illegally.")
  154. }
  155. }
  156.  
  157. //reasonable code ending
  158. if string(data[len(data) - 1]) != ";" && string(data[len(data) - 1] ) != "}" || isOperator(string(data[len(data) - 1])) {
  159. return errors.New("[SYNTAX] Your code doesn't end properly.")
  160. }
  161.  
  162. return nil
  163. }
  164.  
  165. func isOperator(potential string) bool {
  166. if potential == "+" || potential == "-" || potential == "*" || potential == "/" || potential == "^" || potential == "%" || potential == "==" || potential == "!=" || potential == "<=" || potential == ">=" || potential == ">" || potential == "<" || potential == "&&" || potential == "||" || potential == "^^" || potential == "!!" || potential == "!" || potential == "+=" || potential == "-=" || potential == "*=" || potential == "%=" || potential == "^=" || potential == "@=" || potential == "@" {
  167. return true
  168. } else {
  169. return false
  170. }
  171. }
  172.  
  173. func isOperatorNotIncludingMinus(potential string) bool {
  174. if isOperator(potential) && potential != "-" {
  175. return true
  176. } else {
  177. return false
  178. }
  179. }
  180. /* LEXING */
  181. // This is done to catagorize each slice, and organize the slices into tree structures.
  182. func lex(sliced []string) ([]Statement, error) {
  183. statements := make([]Statement,0,0)
  184. for x := 0; x < len(sliced); x ++ {
  185.  
  186. }
  187. return statements, nil
  188. }
  189.  
  190. /* SECONDARY EXCEPTION CHECK*/
  191. // This is done to check for exceptions that are faster to find after lexing is finished.
  192. func secondaryExceptionCheck(statements []Statement) error {
  193. //variables are only in their scopes
  194.  
  195. //operators are surrounded by usable datatypes
  196.  
  197. //reasonable statement beginning
  198.  
  199. return nil
  200. }
  201.  
  202. /* EXECUTION */
  203. // This is done to acually execute the code, which should have been prepared in previous steps.
  204. func execute(statements []Statement, scoped bool) error {
  205. return nil
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement