ChubakPDP11+TakeWithGrainOfSalt

  • 33 Posts
  • 64 Comments
Joined 9 months ago
cake
Cake day: February 10th, 2024

help-circle





























  • The advice offered by Steele in this video no longer applies. It’s still a bit more up-to-date than Kernighan’s talk of a similar title. The fun of this video is in how he twists the English language. He’s truly an erudite man.

    The reason this advice no longer applies is, that I, as a person trying to enter the world of langdev, at least personally, see no reason on defining a new language. I think we should find new ways to describe languages we already have i.e. implement them.

    I am currently making a C compiler in OCaml, besides some other languages. I just began work on the AST tags. I somehow decided to use SSA versioning here.

    But descring a new compiler for C, it’s blaze. I only do it because C is easy, at least non-intrinsicaly. For example, there’s no automatic GC. There are no first-class functions, or function literals (aka ‘lambas’, although this term is massively ‘disused’ — function literals are one thing, lambda literals are one thing, lambda expressions are one thing, function expressions are another thing and so on and so forth – and I don’t have classical education, I just managed to understand that imperative languages abuse the term ‘lambda’ to a dangerous degree. They have named their literals after the concept which it derives from ,it’s like calling binary computers, not the concept, the literal thing, 'Neumann machines, right? Like, go to Best Buy and say ‘Give me this Neumnan machine to play games on’! Maybe that’s because I am too uneducated that I think that, anyways).

    Besides that, there are:

    1- Too many old languages that could use a new veneer, like a SML compiler that uses MLIR or LLVM. 2- There are too many interpreted languages that could use being jitted. Like Ruby. Not sure if there’s jitted Ruby, but I just discoevered how sweet it is and I like a faster version 3- We could dig a mass grave and bury every Python user alive, after torturing them (I’m kidding! lol)

    So I’m not very educated, I brute-force. I rely on ChatGPT models to spit facts at me, or give me validation on my work because I kinda need a ‘college simulator’. Like, I figure, I don’t have nay peers so let’s make this bot my peer.

    In the realm of DSLs, let’s look at a successful example of ‘re-description’: Fish.

    Fish is truly a marvel. Ever since I switched to it, you can’t beleive how faster I work. I don’t know if there were interactive-friendly shells before Fish, but Fish is ‘Friendly’ you know?

    I am implementing my own shell too.

    I dunno man. I’m just rambling.

    Thanks.




  • Thanks a lot my good man. Is this Monte? https://github.com/monte-language/monte I can’t find the source code?

    Can you tell me what you think of the code my implementation emits when you got the time? Is it good, bad, medicore etc?

    So when you’re building a language, should you always use a builder for the AST specifications? Becuase I figure, you don’t need that in a language like Haskell or OCaml right?

    I currently have 3 projects I ping-pong.

    1- Marsh, a POSIX shell in C; 2- Awk2X; a translator of AWK to several languages, like C, Python Rust etc. This one is in Haskell 3- Cephyr; a C compiler in OCaml

    For Marsh, I am stuck in the job control section. I just can’t seem to find a flow for redirecting input and output. I think I am striking a balance though. I revised the job control like 10 times, literally 10 times.

    But for Awk2X and Cephyr, I am kinda stuck at defining the AST. I learned Haskell just last night and I made this AST. I am not ‘stuck’ really, I will move on from it soon.

    data UnaryOp
      = Prefix PrefixOp
      | Postfix PostfixOp
      deriving (Show)
    
    data PrefixOp
      = PrefixIncr
      | PrefixDecr
      | Plus
      | Minus
      | Not
      deriving (Show)
    
    data PostfixOp
      = PostfixIncr
      | PostfixDecr
      deriving (Show)
    
    data BinaryOp
      = Add
      | Sub
      | Mul
      | Div
      | Mod
      | Eq
      | Ne
      | Gt
      | Ge
      | Le
      | Lt
      | And
      | Or
      deriving (Show)
    
    data Lvalue
      = Unfixed String
      | Fixed String String
      deriving (Show)
    
    data Factor
      = Constant Constant
      | Lvalue Lvalue
      deriving (Show)
    
    data Constant
      = String String
      | Integer Int
      | Float Float
      | Regex String
      deriving (Show)
    
    data Expr
      = Unary UnaryOp Factor
      | Binary Factor BinaryOp Factor
      | Ternary Expr Expr Expr
      | StrCat [Expr]
      | Delete Expr
      | Call String [Expr]
      | Assign Lvalue Expr
      deriving (Show)
    
    data Stmt
      = Express Expr
      | For Expr Expr Expr Stmt
      | ForIn Lvalue Lvalue Stmt
      | DoWhile Stmt Expr
      | While Expr Stmt
      | If Expr Stmt [(Expr, Stmt)] (Maybe (Expr, Stmt))
      | Return (Maybe Expr)
      | Break
      | Continue
      | Compound [Stmt]
      deriving (Show)
    
    data Pattern
      = Begin
      | End
      | Expr Expr
      | ExprPair Expr Expr
    
    data PatternAction
      = WithPattern Pattern [Stmt]
      | JustAction [Stmt]
    
    data FunctionDefn = FunctionDefn
      { name :: String,
        params :: [String],
        body :: [Stmt]
      }
    
    data Element
      = PattAct PatternAction
      | FnDef FunctionDefn
    
    newtype Program = Program [Element
    

    Now for Cephyr, this one is a bit more complex. Before Cephyr I attempted to make a C compiler several times, and every time I got stuck at the AST.

    The problem is, I wanna use OCaml’s modules in the AST. But I don’t know how? I just said fuck it and used top-level types. I am here currently:

    type const_expr
    type type_decl
    type unary_op
    type binary_op
    type expr
    type stmt
    
    type literal =
        ConstLiteral of const_lit | CompoundLiteral of compound_lit
    
    and const_lit =
      | IntConst of int * int_suffix option
      | FloatCOnst of float * float_suffix option
      | CharConst of char * charset_prefix option
      | StrConst of string * charset_prefix option
      | IdentConst of string * const_expr
    
    and int_suffix =
        Long | LongLong | Unsigned | UnsignedLong | UnsignedLongLOng
    
    and float_suffix =
        Double | LongDouble
    
    and charset_prefix =
        Local | U8 | U16 | U32
    
    and compound_lit =
      | ArrayCompound of init_type * init_item list
      | StructCompound of init_type * init_item list
      | UnionCompound of init_type * init_item list
    
    and init_item =
      | RegularInit of expr
      | DesignatedInit of designated_init * expr
    
    and designated_init =
      | ConstExpr of const_expr
      | ConstExprPair of const_expr * const_expr
      | Ident of string
    
    and init_type = type_decl
    
    
    type const_expr =
      | UnaryConstExpr of { factor: literal; op: unary_op; }
      | BinaryConstExpr of { left_factor: literal; right_factor: literal; op: binary_op; }
    
    
    type primary_factor =
        Identifier of string | NestedExpr of expr | Literal of literal
    
    and expr =
      | Primary of primary_factor
      | Unary of { factor: primary_factor; op: unary_op; }
      | Subscript of { factor: primary_factor; op: subscript_op; }
      | Binary of { left_factor: expr; right_factor: expr; op: binary_op; }
      | Ternary of { cond: expr; if_true: expr; if_false: expr; }
      | Assignment of { lvalue: expr; rvalue: expr; op: assign_op; }
    
    and prefix_unary_op =
      | Negate            
      | LogicalNegate     
      | BitwiseNot        
      | AddressOf         
      | Dereference       
      | PreIncrement      
      | PreDecrement      
    
    and postfix_unary_op =
      | PostIncrement     
      | PostDecrement
    
    and unary_op =
      | PrefixOp of prefix_unary_op
      | PostfixOp of postfix_unary_op
    
    and subscript_op =
      | Index of expr
      | DotMember of expr
      | ArrowMember of expr
      | FunctionCall of expr list
    
    and binary_op =
      | Add               
      | Subtract          
      | Multiply          
      | Divide            
      | Modulo            
      | BitwiseAnd        
      | BitwiseOr         
      | BitwiseXor        
      | ShiftLeft         
      | ShiftRight        
      | LogicalAnd        
      | LogicalOr         
      | Equal             
      | NotEqual          
      | LessThan          
      | LessThanOrEqual   
      | GreaterThan       
      | GreaterThanOrEqual 
    
    and assign_op =
      | Assignment        
      | AddAssign         
      | SubtractAssign    
      | MultiplyAssign    
      | DivideAssign      
      | ModuloAssign      
      | AndAssign         
      | OrAssign          
      | XorAssign         
      | ShiftLeftAssign   
      | ShiftRightAssign
    

    It’s still incomplete. I just found out I can use .mli files.

    I think Cephyr is the 5th reincaation of my OCaml C compiler. I just spend hours at the AST and get tired of it.

    I found lcc, by Fraiser et al:

    https://github.com/drh/lcc

    And I have the book too. I like the book but it’s kinda useless for me because I wanna do SSA. These kinda tree-rewriting mumbo jumbo is too 80s for my taste.

    So any help is appreciated. Thanks.